CoreData内にString型で保存されてる整数があるのですが、
それを取り出し、合計値を計算して出力するにはどうすればいいでしょうか?
とりあえず値が取り出せるか確認するため以下のコードを書いたのですが、
sum.append(data.num)の行で「Value of type 'AnyObject?' has no member 'num'」
とエラーが出てしまいます。
Swift
1var sum: [NSString] = [] 2 3... 4 5 func readData() { 6 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 7 let managedContext = appDelegate.managedObjectContext 8 let fetchRequest = NSFetchRequest(entityName: "Entity") 9 fetchRequest.returnsObjectsAsFaults = false 10 11 do { 12 let results = try managedContext.executeFetchRequest(fetchRequest) 13 sum = [] 14 15 for data in results { 16 sum.append(data.num) 17 } 18 19 print(sum) 20 21 } catch let error as NSError { 22 print("Could not fetch \(error), \(error.userInfo)") 23 } 24 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/12 07:32