4

I am working on an app that pulls data from a URL.

Here is my code

import UIKit

class ViewController: UIViewController {

@IBOutlet var tellMe: UITextView!
var dataValues: NSArray = []


override func viewDidLoad() {
    super.viewDidLoad()
    loadData()
    // Do any additional setup after loading the view, typically from a nib.
}
func loadData(){
    let dataUrl = NSURL(string: "http://fitgym.mynmi.net/fitgymconnection.php")
    let theData = NSData(contentsOf: dataUrl! as URL)
    let dataLine = dataValues[0]
    dataValues = try! JSONSerialization.jsonObject(with: theData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
    tellMe.text = (dataLine as AnyObject)["cap_percentage"] as? String
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

In addition to this, I have added "App Transport Security Settings" and within that "Allow Arbitrary Loads" is set to "YES"

As it stands right now, I am just trying to pull the information from the URL and display it in my text view named tellMe

Whenever I run this code, I get a SIGABRT error.

Within the console I am seeing:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001099d434b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010943c21e objc_exception_throw + 48
    2   CoreFoundation                      0x00000001099ebddd -[__NSArray0 objectAtIndex:] + 93
    3   DatabaseOne                         0x0000000108daf67a _TFC11DatabaseOne14ViewController8loadDatafT_T_ + 346
    4   DatabaseOne                         0x0000000108daf495 _TFC11DatabaseOne14ViewController11viewDidLoadfT_T_ + 101
    5   DatabaseOne                         0x0000000108daf502 _TToFC11DatabaseOne14ViewController11viewDidLoadfT_T_ + 34
    6   UIKit                               0x0000000109f9906d -[UIViewController loadViewIfRequired] + 1258
    7   UIKit                               0x0000000109f994a0 -[UIViewController view] + 27
    8   UIKit                               0x0000000109e63045 -[UIWindow addRootViewControllerViewIfPossible] + 71
    9   UIKit                               0x0000000109e63796 -[UIWindow _setHidden:forced:] + 293
    10  UIKit                               0x0000000109e770a9 -[UIWindow makeKeyAndVisible] + 42
    11  UIKit                               0x0000000109df0259 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
    12  UIKit                               0x0000000109df63b9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
    13  UIKit                               0x0000000109df3539 -[UIApplication workspaceDidEndTransaction:] + 188
    14  FrontBoardServices                  0x000000010d78076b __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    15  FrontBoardServices                  0x000000010d7805e4 -[FBSSerialQueue _performNext] + 189
    16  FrontBoardServices                  0x000000010d78096d -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    17  CoreFoundation                      0x0000000109979311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x000000010995e59c __CFRunLoopDoSources0 + 556
    19  CoreFoundation                      0x000000010995da86 __CFRunLoopRun + 918
    20  CoreFoundation                      0x000000010995d494 CFRunLoopRunSpecific + 420
    21  UIKit                               0x0000000109df1db6 -[UIApplication _run] + 434
    22  UIKit                               0x0000000109df7f34 UIApplicationMain + 159
    23  DatabaseOne                         0x0000000108db1a9f main + 111
    24  libdyld.dylib                       0x000000010cfef68d start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

What am I doing wrong here?

1 Answer 1

3

You are trying to fetch data from an empty array at let dataLine = dataValues[0] and hence you are getting the error:

index 0 beyond bounds for empty NSArray

Add that line after you get the data in dataValues.

Hence, it should be like:

dataValues = try! JSONSerialization.jsonObject(with: theData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
let dataLine = dataValues[0]
Sign up to request clarification or add additional context in comments.

1 Comment

That was it! Thank you very much!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.