所以我刚刚将我的核心数据模型转换为代码,以便它可以轻松地适应静态库。不幸的是,它不是一个顺利的转换。
尝试插入一个新的 Player 作为另一个名为 gameToPlayer 的类的关系时出现此错误。
Unacceptable type of value for to-one relationship: property = "player"; desired type = (null); given type = Player; value = <Player: 0x178ba9e0>
这是 GameToPlayer 的创建:
gameToPlayerEntity = [[NSEntityDescription alloc] init];
[gameToPlayerEntity setName:@"GameToPlayer"];
[gameToPlayerEntity setManagedObjectClassName:@"GameToPlayer"];
gameToPlayerPlayer = [[NSRelationshipDescription alloc] init];
[gameToPlayerPlayer setDestinationEntity:playerEntity];
[gameToPlayerPlayer setInverseRelationship:playerGames];
[gameToPlayerPlayer setDeleteRule:NSNullifyDeleteRule];
[gameToPlayerPlayer setOptional:YES];
[gameToPlayerPlayer setName:@"player"];
[gameToPlayerPlayer setMaxCount:1];
[gameToPlayerPlayer setMinCount:1];
和玩家创建:
playerEntity = [[NSEntityDescription alloc] init];
[playerEntity setName:@"Player"];
[playerEntity setManagedObjectClassName:@"Player"];
playerGames = [[NSRelationshipDescription alloc] init];
[playerGames setDestinationEntity:gameToPlayerEntity];
[playerGames setInverseRelationship:gameToPlayerPlayer];
[playerGames setDeleteRule:NSCascadeDeleteRule];
[playerGames setName:@"games"];
[playerGames setOptional:YES];
[playerGames setMaxCount:0];
[playerGames setMinCount:0];
崩溃发生的地方(很抱歉所有的 NSLogs 都在使用它们来追踪它)它在我们设置播放器的地方停止:
NSLog(@"Create the GameToPlayer");
GameToPlayer *orderObj = [NSEntityDescription insertNewObjectForEntityForName:@"GameToPlayer" inManagedObjectContext:[SDDataManager dataManager].managedObjectContext];
NSLog(@"Set the Game");
[orderObj setGame:newGame];
NSLog(@"Set the Player");
[orderObj setPlayer:player];
NSLog(@"Set the Order");
[orderObj setOrder:[NSNumber numberWithInt:[selectedPlayers indexOfObject:player]]];
NSLog(@"Add to Array");
[orderObjs addObject:orderObj];
现在我真的觉得我知道问题所在了。在我的 ManagedObjectModel 的自定义初始化中。 gameToPlayerEntity 在 playerEntity 之前创建。所以当创建 gameToPlayerEntity 时,playerEntity 实际上是 NULL。但是我不能简单地重新排序,使它们成为属性,或做任何事情来修复它,因为如果我更改文件中的任何内容,它似乎不再能够映射现有数据库...
而且我在文档中找不到任何地方,它显示了如何对 xml 模型等代码模型进行版本控制,我认为这是问题所在。任何帮助或提示表示赞赏。只是想了解如何让它发挥作用。
更新
认为值得一提的是,在此转换之前,我已经在使用轻量级迁移并且正在进行数据库的第 4 次迭代。
更新 2
我已经能够通过完全重新开始来让它工作。而不是试图从已发布的内容继续前进。一切都已转换为属性,因此每个实体都在需要时创建,并且只创建一次。这相当于从可视化模型到代码模型的转换。 但是当这个模型不可避免地发生变化时,我将来肯定需要了解版本控制。版本控制是这个问题的根源。因此,如果有人可以阐明如何在不使用 xcode 界面的情况下进行版本控制,我很想听听您的想法。
最佳答案
我认为这不一定是您的问题,但我确实想为同样遇到此错误的人们发布另一种可能性。如果您在将数据从一个持久存储移动到另一个(例如,从后备存储移动到 iCloud 存储)时看到此错误,则您用于创建新托管对象的 NSEntityDescription 很可能来自您的旧副本对象(因此是错误的托管对象上下文)。
当你像这样编写代码时:
NSEntityDescription *entity = [oldObj entity];
YourClass *newObj = [[YourClass alloc] initWithEntity:entity
insertIntoManagedObjectContext:moc];
您需要采取额外的步骤从要插入的上下文中获取实体描述。
NSEntityDescription *entity = [oldObj entity];
NSEntityDescription *goodEntity = [NSEntityDescription entityForName:entity.name
inManagedObjectContext:moc];
YourClass *newObj = [[YourClass alloc] initWithEntity:goodEntity
insertIntoManagedObjectContext:moc];
感谢 Pavel Kapinos 在 Cocoa Dev 电子邮件列表中的注释。 http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg24863.html
关于objective-c - 一对一关系的值类型是 Not Acceptable 。所需类型=(空);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12039160/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s