-1

我在更改 NSString 的值时遇到问题。

它在我的课堂上是这样声明的:

@property (strong, nonatomic)NSMutableString *votes;

创建对象时,它的设置如下:

song.votes = [dict objectForKey:@"Votes"];

最后是问题发生的地方。稍后在我的代码中,我尝试像这样修改值:

song.votes =[responseArr valueForKey:@"CNT"];

这条线导致了这次崩溃:

'NSInvalidArgumentException',原因:'-[__NSCFString setVotes]:无法识别的选择器发送到实例 0x14f84430'

我认为我的问题是由以下之一引起的:

1. 错误地设置了上面的属性。我也尝试过设置它,(copy, nonatomic)但它做同样的事情。

2. 我需要为此使用 NSMutableString。我尝试将其更改为 NSMutableString,但是当它更改时它仍然崩溃(诚然,我正在以使用 NSMutableString 时的方式进行初始化和更改,我不完全确定当它是 Mutable 时如何更改它。

4

2 回答 2

1

I think the problem is in the way you're allocating / setting your song object. Somewhere between setting the first and the second value, you're probably deallocating song and then trying to set it's properties, or you're modifying it in such a way that it's not the same class type anymore.

unrecognized selector sent to instance 0x14f84430' pretty much sums it up for you. The second time you try to set the votes property, it tries to access the synthesized setter (setVotes) from song which is no longer the class you think it is.

From the error it looks like you may be re-allocating song as a NSString object. That's why it's trying to access a setVotes method on NSString and such a method does not exist, so it bails out and crashes. Are you sure you're not doing something like song = [someString retain]; ?

于 2013-08-27T03:20:48.080 回答
1

Use -mutableCopy if you need a mutable copy of your NSString.

song.votes = [[dict objectForKey:@"Votes"] mutableCopy];

Assuming responseArr is an array, [responseArr valueForKey:@"CNT"] returns an array with the return value of each of the instances in responseArr. Your property is for a NSMutableString, but you set it to a NSArray.

(Also, do provide the actual error that you get when you crash instead of just saying 'it crashes'.)

于 2013-08-27T03:21:05.297 回答