我正在开发一个从网址中提取数据的应用。
这是我的代码
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.
}
}
除此之外,我还添加了"应用传输安全设置"在那个"允许任意负载"设置为"是"
现在看来,我只是试图从URL中提取信息并将其显示在名为tellMe的文本视图中
每当我运行此代码时,都会收到SIGABRT错误。
我在控制台内看到:
*** 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
我在这里做错了什么?
答案 0 :(得分:1)
您正在尝试从let dataLine = dataValues[0]
的空数组中获取数据,因此您收到错误:
索引0超出空NSArray的边界
在dataValues
中获取数据后添加该行。
因此,它应该是:
dataValues = try! JSONSerialization.jsonObject(with: theData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
let dataLine = dataValues[0]