xcode 不可接受的属性值类型:property
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9029327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unacceptable type of value for attribute: property
提问by priya
I created my app using Core Data, declared 2 attributes, SongLyrics and MovieSongName, both as string. Then in xib I created a text field for MovieSongName and a text view for SongLyrics. For storing these I used the following code
我使用 Core Data 创建了我的应用程序,声明了 2 个属性,SongLyrics 和 MovieSongName,两者都是字符串。然后在 xib 中,我为 MovieSongName 创建了一个文本字段,并为 SongLyrics 创建了一个文本视图。为了存储这些,我使用了以下代码
-(void)saveData
{
LyricsAppDelegate *appDelegate = [[UIApplication sharedApplication]
delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription
entityForName:@"Lyrics"
inManagedObjectContext:managedObjectContext];
NSManagedObject *LyricsObjectEnglish;
LyricsObjectEnglish = [NSEntityDescription
insertNewObjectForEntityForName:@"English_Songs"
inManagedObjectContext:managedObjectContext];
[LyricsObjectEnglish setValue:song_lyrics.text forKey:@"SongLyrics"];
[LyricsObjectEnglish setValue:song_name forKey:@"MovieSongName"];
song_lyrics.text=@"";
song_name.text=@"";
NSError *error;
[managedObjectContext save:&error];
}
when clicking on the save button app gets aborted with the following error.
单击保存按钮时,应用程序因以下错误而中止。
2012-01-27 10:50:52.071 Lyrics[4624:207] *Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "MovieSongName"; desired type = NSString; given type = UITextField; value = >.'
2012-01-27 10:50:52.071 歌词[4624:207] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不可接受的属性值类型:property =“MovieSongName”;所需类型 = NSString; 给定类型 = UITextField; 值 = >.'
Can any one help me? I'm using Core Data for the first time and I'm a little confused.
谁能帮我?我是第一次使用 Core Data,我有点困惑。
回答by UIAdam
The error is telling you exactly what the problem is. Specifically, MovieSongName needs an NSString
value, but the value you are giving it is a UITextField
because you are trying to give it song_name
rather than song_name.text
.
该错误准确地告诉您问题是什么。具体来说, MovieSongName 需要一个NSString
值,但您赋予它的值是 aUITextField
因为您试图赋予它song_name
而不是song_name.text
。
The correct code is:
正确的代码是:
[LyricsObjectEnglish setValue:song_name.text forKey:@"MovieSongName"];