从 NSMutableArray 转换对象 - Objective c
我是 Objective-C 的新手,从 8 年前的大学时代起就没有编程过。
我想知道如何将对象从可变数组转换为整数,以便我可以对其进行计算。
NSMutableArray *years = [NSArray arrayWithObjects:@"1800",@"1801",@"1899",@"1900",@"2000",@"2001",@"2003",@"2010",nil];
int count;
count = years.count;
for(int i = 0 ; i < count ; i++)
{
NSLog(@"The year %@ ", [years objectAtIndex:i]);
}
谢谢
I'm new to objective-c and havent programmed since my college days 8 years ago.
I wondering how do i convert an object from a mutable array to a integer so i can do a calculation to it.
NSMutableArray *years = [NSArray arrayWithObjects:@"1800",@"1801",@"1899",@"1900",@"2000",@"2001",@"2003",@"2010",nil];
int count;
count = years.count;
for(int i = 0 ; i < count ; i++)
{
NSLog(@"The year %@ ", [years objectAtIndex:i]);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看
NSString
文档。要将
NSMutableArray
中的对象转换为整数,只需执行此外,您将
years
定义为NSMutableArray
但随后创建一个NSArray
。你的第一行应该是最后,迭代数组的更好方法如下:
Take a look at the
NSString
documentation.To convert the objects in your
NSMutableArray
to an integer, just doAlso, you define
years
as anNSMutableArray
but then you create anNSArray
. You're first line should beAnd finally, a better way to iterate through the array would be like this:
您可以查看 NSString 类参考,它会告诉您有一个可以传递字符串的
intValue
消息。如果您的字符串是有效数字,您将得到一个int
返回。例如,你可以这样做:
You would look at the NSString class reference, which will tell you that there's an
intValue
message you can pass strings. If your strings are valid numbers, you'll get anint
back.For instance, you could do something like this:
例如,但是不涉及错误处理:
看看 NSString的文档,还有一个将字符串转换为float(floatValue)的方法。
For example, however there is no error handling involved:
Have a look at the documentation of NSString, there is also a method to convert a string into a float(floatValue).