2017-01-26 11 views
0

URLからデータを取得するアプリを開発中です。ここでSwiftのURLのデータ

は私のコードは、これに加えて

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. 
} 


} 

ですが、私は現状では「アプリのトランスポート・セキュリティの設定」と、その中に「任意のロードを許可するが、」

「YES」に設定されている追加されました今すぐ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

あなたはlet dataLine = dataValues[0]で空の配列からデータを取得しようとしているので、あなたは、エラーを取得している:あなたはdataValues内のデータを取得した後

index 0 beyond bounds for empty NSArray

その行を追加します。したがって

、それは次のようにする必要があります:それはされたことを

dataValues = try! JSONSerialization.jsonObject(with: theData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray 
let dataLine = dataValues[0] 
+0

!どうもありがとうございました!! – Darthmaul