SWIFT COREDATA-循环通过具有“for in”的结果。循环构建单独的数组 [英] SWIFT COREDATA - looping through results with "for in" loop to build separate array
问题描述
SWIFT COREDATA
我在设置for-in循环以从抓取中收集结果时遇到问题。没有问题通过fetch results [index]获取单个结果,但插入for-in循环给我一个错误(类型不匹配)或(类型不包含生成器中的成员)。我想使用抓取结果填充单独的数组。
SWIFT COREDATA I'm having trouble setting up a for - in loop to collect results from a fetch. No problem getting individual results by fetch results[index] but inserting for-in loop gives me an error (type mismatch) or (type does not contain member in Generator). I want to use the fetch results to populate a separate array.
非常感谢您提供的任何帮助。
Any help you can provide is greatly appreciated.
摘录自代码:
import Foundation
import UIKit
import CoreData
// globaals
var pickResult:String?
var pickMultiresult:[AnyObject]?
// add var
var fullnameMulti:[String]? // array of full names
var i:Int = 0
// end new var
class ClientPicker:UIViewController, UIPickerViewDelegate {
// connections
@IBOutlet weak var singleResult: UITextField!
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
@IBOutlet weak var firstname: UITextField!
@IBOutlet weak var status: UILabel!
var lastname:String = ""
var phone:String = ""
var email:String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// MARK: ********** block 2 --- findClient ****
@IBAction func findClient(sender: AnyObject) {
let entityDescription = NSEntityDescription.entityForName("Contacts", inManagedObjectContext: managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
let pred = NSPredicate(format: "(firstname = %@)", firstname.text!)
request.predicate = pred
do {
var results = try managedObjectContext.executeFetchRequest(request)
multiresult = results
if results.count > 0 {
// let match = results[0] as! NSManagedObject NOTE: these return correct data for indices 0 and 3
// let match = results[3] as! NSManagedObject NOTE: these return correct data for indices 0 and 3
status.text = "There are \(results.count) people named \(firstname.text!)"
} else {
status.text = "No Match"
}
} catch let error as NSError {
status.text = error.localizedFailureReason
}
// for in loop to populate firstnameMulti array
i = 0
for i in results{
var p = i
let match = results[p] as! NSManagedObject
/* the following code returns correct fullname when used outside of for loop
but error when I use the for loop */
firstname.text = match.valueForKey("firstname") as? String
/***************** concatenate first and last names ********/
let first = match.valueForKey("firstname") as? String
let last = match.valueForKey("lastname") as? String
// concatenate
var fullname:String = ""
fullname = "\(first!) \(last!)"
singleResult.text = fullname
}
}
*/ error: value of type string has no member in Generator */
推荐答案
您正在填充循环中的文本字段的文本,并在每次迭代时覆盖它。这没有任何意义。
You are filling the text of a text field in the loop and overwriting it at every Iteration. That does not make any sense.
要生成全名数组,请获取您的对象并转换为适当的NSManagedObject子类,例如 Person
所以你得到一个 [Person]
数组,然后你可以使用 map
:
To generate an array of full names, fetch your objects and cast to the proper NSManagedObject subclass, such as Person
so you get a [Person]
array and then you can simply use map
:
let nameList = results.map { "\($0.firstname) \($0.lastname)" }
这篇关于SWIFT COREDATA-循环通过具有“for in”的结果。循环构建单独的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!