まだコアデータ関係に問題があります。Coredata/SWIFTは関係があるレコードを取得します
例:
エンティティユーザー エンティティ装置(=名前の属性IDDを、)(IDU、NameU =属性)
core data model 質問:私は、新しいデバイスを作成するときに、私はNssetの問題を持っていますユーザーにリンクされています。
コード:
@IBAction func addDeviceBoutonAction(sender: AnyObject) {
print("Creation Device debut")
var nbCharacteres = Int((newDeviceNameTextField.text?.characters.count)!)
if currentUserName == "" {
print("Erreur creation device/currentUserName = Vide --> Abort")
}
else if nbCharacteres == 0 {
print("Erreur creation device/newDeviceNameTextField = Vide --> Abort")
}
else
{
// init du MOC
let MOC = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// creation de la requete sur l'entité User avec un predicat : on recupere que les infos du user selectionné dans le pickerViewUser (on
// travaille avec la variable globale currentUserName qui a été initialisée lors du didSelectRow du pickerViewUser)
let fetchRequestUser = NSFetchRequest(entityName: "User")
print("Creation device/trace currentUserName = \(currentUserName)")
let userPredicate = NSPredicate(format: "userName == %@", currentUserName)
// On colle le predicat dans la requete
fetchRequestUser.predicate = userPredicate
var requestError: NSError?
let existingUser = (try! MOC.executeFetchRequest(fetchRequestUser)) as! [User]
print("Creation device/trace existingUser[0].userName = \(existingUser[0].userName)")
// il faut passer par l'ID plutot que le nom
// firstUserFound de type User
let firstUserFound = existingUser[0]
print("Creation device firstUserFound = \(firstUserFound)")
// Insertion d'un nouveau Device dans la base (relationship n to n entre Device et User)
let existingDevicesForThisUser = firstUserFound.mutableSetValueForKey("device")
// Create Item Record
let newDevice = NSEntityDescription.insertNewObjectForEntityForName("Device",inManagedObjectContext: MOC) as! Device
//if let newDevice = createRecordForEntity("Device", inManagedObjectContext: MOC) {
// Set Attributes and values
newDevice.setValue(NSUUID().UUIDString, forKey: "deviceId")
newDevice.setValue(newDeviceNameTextField.text, forKey: "deviceName")
newDeviceNameTextField.text = ""
self.view.endEditing(true)
//newDevice.setValue("Device \(newDevice.count + 1)", forKey: "deviceId")
// Set Relationship
print("avant relationhips")
newDevice.setValue(firstUserFound, forKey: "users")
エラー: 作成デバイスデビュー
私はそれがまだ働いていないUPDATE多く、多くの関係に関連していると思います
作成デバイス/トレース電流ユーザー名= TOTO
作成装置/ existingUserトレース[0] .userName =オプション( "TOTO")
作成装置firstUserFound =(エンティティ:ユーザー。 ID:0xd000000000080000;データ:{ devices = ""; userId = "8A32EAC6-87E2-4CF1-8265-5E7A32ABCEBB"; userName = toto; 55:13.065 testcoredatarelationships [1568:370426] })
前衛は
2016年4月2日13 relationhips *によりキャッチされない例外 'NSInvalidArgumentException'、理由にアプリを終了:「への値の許容できないタイプ - 多少の関係:property = "users";所望のタイプ= NSSet;与えられた型= testcoredatarelationships.User;値=(エンティティ:ユーザー; ID:0xd000000000080000;データ:{ デバイス= ""; userId = "8A32EAC6-87E2-4CF1-8265-5E7A32ABCEBB"; userName = toto; })。 *まずスローコールスタック:間違っている
ライン: newDevice.setValue(firstUserFound、forKey: "ユーザー")
拡張デバイス{
@NSManaged var deviceId: String?
@NSManaged var deviceName: String?
@NSManaged var cards: NSSet?
**@NSManaged var users: NSSet?**
}
あなたはすでにJackインスタンスをお持ちですか?あなたの関係は双方向ですか(そうすべきですか)?エンティティモデルの画像を表示します。 – Wain
こんにちは、実際には、それはちょうどダミーのデータです。私はちょうどエンティティ間の関係の船が動作しているかを理解するために、実際の例を作成したいと思います(一部のフィールドが異なっているが、それは[OK]をする必要があり) – h3630fr
コアモデルを置きます。そう( – h3630fr