开发者

NSString unexpectedly becomes __NSCFDictionary

I have this very strange problem, I'm new to objective-c and it probably comes from depths which I don't comprehend yet.

So, in my header file I declare the variable

NSString *curTitle;

then in .m file I synthesize it:

curTitle = [[NSString alloc] init];

after that in other method I assign it:

curTitle = string; // string is an instance of NSString

and at the end of the day when I'm trying to assign

slide.title = curTitle; //slide is a managed object (CoreData)

I'm getting this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "title"; desired type = NSString; given type = __NSCFDictionary; value = { }.'

Interesting fact that in iphone SDK 3.2 it worked, but after I installed SDK 4 I have this error

Another interesting fact that if make 开发者_如何学Cmy curTitle property of my class (with @property and @synthesize) it also works

Any ideas? Thanks


When an object seems to change class, it's almost always because it has been deallocated, and another object is now residing on the same memory location. All object variables are actually pointers to memory locations (that's what the * means), and unlike in many other languages you have to always remember that you're working with memory locations. (Well, not always, once you get the hang of it. But definitely when you're debugging.)

To debug these problems, it can be very useful to use NSZombie. Just search SO or the web for info.

then in .m file I synthesize it: curTitle = [[NSString alloc] init];

This is not synthesizing. What you are doing here is merely assigning a value to the variable. More precisely, you create an object somewhere in memory (alloc) and initialize it (init) and then you set the value of curTitle to point to that memory location.

There is no need for you to have this line in your code at all.

curTitle = string;

Here you are overwriting the old value of curTitle with a pointer to another string. Your problem is most likely that that other string later gets deallocated, and the memory location gets reused to hold another object (a dictionary in this case). (If you don't know about the retain/release mechanisms, you need to read up on those to understand what happens.)

slide.title = curTitle;

Since curTitle is pointing to a memory location that has been re-used to hold a random object (or even worse: garbage) this line will fail. In fact, you're lucky that it fails, because if it didn't (if the location hadn't been reused yet) it would be even harder to detect the bug.

What you should do is to declare a property, then synthesize it and access the variable through the property:

self.curTitle = aString;

This will make sure that you actually copy and take ownership of the string so that even if the original string is released, you will still have a valid copy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜