Objective c 如何创建包含NSString和整数的临时对象数组?

Objective c 如何创建包含NSString和整数的临时对象数组?,objective-c,arrays,Objective C,Arrays,在一个简单的Objective-C方法中,我想创建一个临时对象数组,其中每个对象包含两个元素: { NSString *objectName; int objectCount; } 这是一个临时数组,不在方法外部使用,也不在对象接口中定义。如何在Objective-C中定义这样的数组?您可以将其添加到.m文件中的类扩展名中,类似于: @interface YOURCLASSNAME () @property (nonatomic, strong) NSArray *tmpArr

在一个简单的Objective-C方法中,我想创建一个临时对象数组,其中每个对象包含两个元素:

{
    NSString *objectName;
    int objectCount;
}

这是一个临时数组,不在方法外部使用,也不在对象接口中定义。如何在Objective-C中定义这样的数组?

您可以将其添加到.m文件中的类扩展名中,类似于:

@interface YOURCLASSNAME ()
@property (nonatomic, strong) NSArray *tmpArray;
@end
@implementation YOURCLASSNAME {
    NSArray *_tmpArray;
}
_tempArray = ...
如果您想打电话,请执行以下操作:

self.tempArray = ...

也可以将其添加为.m文件中的ivar,如下所示:

@interface YOURCLASSNAME ()
@property (nonatomic, strong) NSArray *tmpArray;
@end
@implementation YOURCLASSNAME {
    NSArray *_tmpArray;
}
_tempArray = ...
你这样称呼它:

@interface YOURCLASSNAME ()
@property (nonatomic, strong) NSArray *tmpArray;
@end
@implementation YOURCLASSNAME {
    NSArray *_tmpArray;
}
_tempArray = ...
//延伸

要将对象添加到阵列中,可以执行以下操作:

CustomObject *obj1 = [[CustomObject alloc] init];
obj1.objectName = @"Name 1";
obj1.objectCount = 1;
CustomObject *obj2 = [[CustomObject alloc] init];
obj2.objectName = @"Name 2";
obj2.objectCount = 2;

_tmpArray = @[obj1, obj2];

如果不想创建自定义对象来存储objectName和objectCount,可以使用NSDictionary

例如:

NSString *objectNameKey = @"objectName";
NSString *objectCountKey = @"objectCount";

NSDictionary *tempObject = [[NSDictionary dictionaryWithObjectsAndKeys:
    @"someName", objectNameKey,
    [NSNumber numberWithInteger:7], objectCountKey, 
    nil];

NSString *objectName = [tempObject objectForKey:objectNameKey];
int objectCount = [tempObject objectForKey:objectCountKey];

我只需定义一个辅助对象:

@interface MyHelper
@property(copy) NSString *name;
@property(assign) unsigned int count;
@end

@implementation MyHelper
@end
就这样。如果不使用ARC,则需要实现
dealloc
以释放
name

您还可以使用数组数组(非常糟糕)或可变字典数组(也很糟糕)。我不建议这样做,因为这比仅仅定义helper对象要复杂得多,而且会丢失上下文。访问第一个对象的名称可能如下所示:

id element = [myArray objectAtIndex:0];

// Helper object
name = element.name; // or [element name]

// Array of arrays
name = [element objectAtIndex:0]; // 0 means "name"... ugh

// Array of dictionaries
name = [element objectForKey:@"name"]; // better than with arrays.
因此,读取名称或计数很容易。但当你想更新某些东西时,事情会变得一团糟:

id element = [myArray objectAtIndex:0];

// Helper object
element.count++; // Easy. Everybody gets it.

// Array of arrays.
// Yuck!
NSNumber *oldValue = [element objectAtIndex:1];
[myArray replaceObjectAtIndex:0 withObject: @[ [element objectAtIndex:0], @([oldValue unsignedIntValue] + 1) ]];
// Or, if the element is actually a mutable array:
[element replaceObjectAtIndex:1 withObject: @([oldValue unsignedIntValue] + 1)];

// Array of MUTABLE dictionaries
// Better, but still ugly.
oldValue = [element objectForKey:@"count"];
[element setObject:@([oldValue unsignedIntValue] + 1) forKey:@"count"];

NSMutableArray*myarray=[NSMutableArray];请原谅我的无知,但这样做的语法是什么?正如你所看到的,我在这里挣扎着学习一些基础知识。我的意思是,一旦我声明了_tmpArray,我如何向它添加int和string对象?我是否需要创建另一个包含这两个元素的数组,然后将该数组添加到tmpArray中?@SillyGoof(1)将其设置为NSMutableArray,而不是NSArray。查看NSMutableArray的文档。(2) 无法将int对象添加到NSArray(或NSDictionary):int不是对象类型。您必须包装一个NSNumber对象。请参见编辑后的答案,这是如何将自定义对象添加到数组中。使用现代语法,您可以将其缩短为:
NSDictionary tempObject=@{objectNameKey:@“someName”,objectCountKey:@(7)}更容易阅读,亲爱的。@DarkDust你甚至不需要在
7
附近的括号,因为它不是一个表达式!您只需使用
@7
@DarkDust即可。谢谢您添加了它。(这称为objective-c文字)