简体   繁体   English

NSMutableArray,int值从1到100

[英]NSMutableArray with int values from 1 to 100

This should be dead easy, but somehow it doesn't want to work for me. 这应该是很容易的,但是以某种方式它不想为我工作。 Using iOS 7 and XCode 5. All I'm trying to do is create an array with values from 1 to 100. 使用iOS 7和XCode5。我要做的就是创建一个值从1到100的数组。

NSMutableArray *array;
for (int i = 0; i < 100; i++)
{
    [array addObject:i];
}

This doesn't work. 这行不通。 I get a "Implicit conversion of 'int' to 'id' is disallowed with ARC. I get it, I can't add primitive types to an NSMutableArray. 我收到“ ARC不允许将'int'转换为'id'的隐式转换。得到的是,我无法将原始类型添​​加到NSMutableArray。

    [array addObject:@i];

This doesn't work either. 这也不起作用。 I get a "unexpected '@' in program" 我在程序中收到“意外的'@'”

    [array addObject:[NSNumber numberWithInt:i]];
    [array addObject:[NSNumber numberWithInteger:i]];

(either case) This "works" (compiles) but it really doesn't "work". (无论哪种情况)此“有效”(编译),但实际上不“有效”。 The problem with this is that the value from NSNumber is really not a 1-100. 问题在于,NSNumber的值实际上不是1-100。 What I get for each row is "147212864", 147212832", "147212840"...not what I want. 我每行得到的是“ 147212864”,“ 147212832”,“ 147212840” ...不是我想要的。

Lastly: 最后:

for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)
{
    [array addObject:i];
}

This also doesn't compile. 这也不会编译。 I get an error on the i++. 我在i ++上收到错误消息。 "Arithmetic on pointer to interface 'NSNumber', which is not a constant size for this architecture and platform" “指向接口'NSNumber'的指针的算法,对于该体系结构和平台,大小不是恒定的”

Any suggestions on how to do this extremely simple thing on obj-c? 关于如何在obj-c上执行此极其简单的操作的任何建议?

Either one of these should work: 这些之一应工作:

NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
    [array addObject:@(i)];
}

or 要么

NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
    [array addObject:[NSNumber numberWithInt:i]];
}

Here are the reasons why your code snippets did not work: 您的代码段无法正常工作的原因如下:

  1. [array addObject:i] - You cannot add primitives to Cocoa collections [array addObject:i] -您无法[array addObject:i]语添加到Cocoa集合中
  2. [array addObject:@i] - You forgot to enclose the expression i in parentheses [array addObject:@i] -您忘记将表达式i括在括号中
  3. NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++ NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++ - You cannot increment NSNumber without "unwrapping" its value first. NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++ -您必须先“解开” NSNumber的值,然后才能增加NSNumber的值。

If memory serves, I think you're simply missing parenthesis around the NSNumber shorthand expression. 如果有内存,我认为您只是在NSNumber速记表达式周围缺少括号。

NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
    [array addObject:@(i)];
}

Minimally, @i should be @(i) as described here . 最低限度, @i应该是@(i)所描述的在这里 You are also forgetting to allocate and initialise your array 您还忘记分配和初始化数组

NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++) {
    [array addObject:@(i)];
}

And since you are getting: "147212864", 147212832", "147212840"...not what I want. , I think you are probably printing out your information wrongly or because the array is unallocated, that's simply garbage. Can you show us how you are outputting? 而且,由于您得到的是: "147212864", 147212832", "147212840"...not what I want. ,我想您可能是错误地打印了您的信息,或者因为数组未分配,这简直是垃圾。您能告诉我们吗?您如何输出?

 NSMutableArray *array = [[NSMutableArray alloc] init];
 NSNumber *myNum;
 for (int i = 0; i < 100; i++) {

     myNum = [[NSNumber alloc]initWithInt:i];
     [array addObject:myNum];
 }
 NSLog(@"%@", array); // 1 - 99 as expected

Worked for me :) 为我工作:)

Just saying: Turn on all reasonable warnings in your Xcode project. 只是说:在Xcode项目中打开所有合理的警告。 Then read what the warnings are saying and do something about them. 然后阅读警告内容,并对其进行处理。 When you write something like 当你写类似

for (NSNumber *i = 0; i < [NSNumber numberWithInteger:100]; i++)

What does a for loop do? for循环有什么作用? An object is in the end a pointer. 最后一个对象是一个指针。 So you initalise i to nil. 所以你把我设为零。 Then you compare a pointer with a random pointer: [NSNumber numberWithInteger:100] returns a pointer to an object which could be anywhere in memory, and you compare pointers. 然后,您将指针与随机指针进行比较:[NSNumber numberWithInteger:100]返回指向对象的指针,该对象可以在内存中的任何位置,然后您可以对指针进行比较。 Next the i++: No, you can't increment a pointer to an NSNumber. 接下来的i ++:不,您不能增加指向NSNumber的指针。 It doesn't make sense. 这没有道理。

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

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