简体   繁体   English

无法获取NSString ivar以接受数组中的值

[英]Can't get an NSString ivar to accept a value from an array

I am storing some data (some floats, some strings) to a plist and then reading them back. 我将一些数据(一些浮点数,一些字符串)存储到plist中,然后将其读回。 When I read them back, I assign them to some ivars in my view controller. 当我读回它们时,我将它们分配给视图控制器中的某些ivars。 They are just ivars, not properties. 它们只是ivars,不是属性。

The floats take their values fine and I can NSLog them and see that they are correct. 浮点数可以很好地使用它们的值,我可以对它们进行NSLog并查看它们是正确的。 But no matter what I try, I can't get my NSString ivars to take the string values. 但是,无论我尝试什么,我都无法让我的NSString ivars接受字符串值。

Example: array positions 6 and 7 have the strings Portland, OR" and "Pacific Daylight Time" stored in them - read back from the plist into *array. 示例:数组位置6和7中存储着字符串Portland, OR""Pacific Daylight Time" -从plist读回*array.

This: 这个:

cityName = [NSString stringWithFormat:@"", [array objectAtIndex:6]];
timeZoneName = [NSString stringWithFormat:@"", [array objectAtIndex:7]];
NSLog(@" >cityName from array = %@, and now the iVar is = %@", [array objectAtIndex:6], cityName);
NSLog(@" >timeZoneName from array = %@, and now the iVar is = %@", [array objectAtIndex:7], timeZoneName);

Results in this: 结果:

>cityName from array = Portland, OR, and now the iVar is = 
>timeZoneName from array = Pacific Daylight Time, and now the iVar is = 

I put a breakpoint at the end of the method where this happens and for both NSString ivars, the little pop up message says, Invalid Summary. 我在发生这种情况的方法的末尾放置了一个断点,对于两个NSString ivars,弹出的小消息都说“ Invalid Summary.

cityName = [NSString stringWithFormat:@"%@", [array objectAtIndex:6]];

The mistake is that you've missed the format specifier. 错误是您错过了格式说明符。 Specifically though, if the objects in the array are already strings, you can just assign them straight to the ivar: 不过,特别是,如果数组中的对象已经是字符串,则可以直接将它们直接分配给ivar:

cityName = [array objectAtIndex:6];

Be careful about object ownership though. 但是要注意对象所有权。 You don't own the object returned by objectAtIndex: so if you need to use this ivar in many different methods, obtain ownership by sending retain , and relinquish ownership using release . 你不能拥有被返回的对象objectAtIndex:所以如果你需要在许多不同的方法来使用这个实例变量,通过发送取得所有权retain ,并采用放弃所有权release Alternatively if you choose to establish cityName as a retaining or copying property of your class, use the dot-notation or accessor method: 或者,如果您选择将cityName建立为类的保留或复制属性,请使用点符号或访问器方法:

self.cityName = [array objectAtIndex:6];

// or

[self setCityName:[array objectAtIndex:6]];

If the array contains NSString instances there is no need to call +stringWithFormat: . 如果数组包含NSString实例,则无需调用+stringWithFormat: It is a waste of CPU cycles and undoes any potential optimizations related to immutable strings. 这浪费了CPU周期,并且取消了与不可变字符串相关的任何潜在优化。 Furthermore, relying upon the results of description is never the right answer (though it is nigh unavoidable with NSStrings -- this is the one spot where you can rely on the result). 此外,依靠的结果description是永远正确的答案(虽然它是不可避免的亲近与NSString的-这是一个地方,你可以依赖的结果)。

This (fixed): 此(固定):

cityName = [NSString stringWithFormat:@"%@", [array objectAtIndex:6]];
timeZoneName = [NSString stringWithFormat:@"%@", [array objectAtIndex:7]];

Could be: 可能:

cityName = [array objectAtIndex:6];
timeZoneName = [array objectAtIndex:7];

Copy it if you need to. 如果需要,请复制它。 As @dreamlax said, make sure you follow the memory management rules. 正如@dreamlax所说,确保您遵循内存管理规则。

Use below 在下面使用

cityName = [NSString stringWithFormat:@"%@", [array objectAtIndex:6]];

You require to use %d or %i for integers and %@ is used for string objects. 您需要将%d或%i用于整数,并将%@用于字符串对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM