ios - 如何将 NSArray 转换为整数值

标签 ios objective-c iphone

在我的 iPhone 中,通过应用以下代码,我可以获得“i”的值

NSMutableArray *Array = [NSMutableArray arrayWithCapacity:100];

for (NSUInteger i = 0; i < 10; i++) {          
    id a = [NSDecimalNumber numberWithDouble:i];
    [Array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:a, @"a", nil]];
 }

但如果我想从数组中获取值,例如

NSArray * numberArray = [[NSArray alloc] initWithObjects:@"0.1",@"0.2",@"0.5",nil];

然后如果我想将它用作

NSMutableArray *Array = [NSMutableArray arrayWithCapacity:100];

for (NSUInteger i = 0; i < 10; i++) {          
    id a = [NSDecimalNumber numberWithDouble:[numberArray objectAtIndex:i]];
    [Array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:a, @"a", nil]];
 }

我该怎么做?

最佳答案

我不太明白,但我会尽力帮助你。

如果您想将整数存储到 NSArray 中,请使用此代码来设置和获取它们:

// Set
NSArray *yourArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3],nil];

// Get
int currentNumber;
for(int i=0; i<[yourArray count]; i++)
{
    currentNumber = [((NSNumber*)[yourArray objectAtIndex:i]) intValue];
}

关于ios - 如何将 NSArray 转换为整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875693/

相关文章:

ios - Uber 身份验证失败 "HTTP Status 401: Unauthorized, Response: {"错误“: "invalid_client"}"

ios - 如何完全卸载 Xcode 并清除所有设置

iphone - 选择正确的方法将图像写入 iOS 文件夹

iOS - 更改 UISegmentController 中每个段的文本字体

iphone - 拖动图像时,请勿将图像移出屏幕

iphone - 链接器命令失败,退出代码为1-不知道为什么

ios - 可以从 web URL 和本地目录按文件名加载图像的扩展

iphone - 错误:访问全局变量和核心数据时,NSArray在枚举时发生了变异

ios - 为 TableView 部分添加背景 iOS

ios - MPMoviePlayerController initWithContentURL 延迟按钮点击