Objective c 目标C中的默认列表?

Objective c 目标C中的默认列表?,objective-c,Objective C,我想在运行时存储数据,我可以有一个链表并在运行时添加,但是由于我对IOS和objective C不熟悉,我们是否有可以添加数据的默认列表(数据是两个Sting和一个整数) Cocoa提供了一对有序容器,类似于Java的ArrayList和C#的List。您可以将值添加到NSMutableArray,它将随着您添加更多元素而增长NSArray是只读的。您可以使用.plist文件来存储数据。 阅读更多,或者像“从.plist加载数据”一样在谷歌上搜索它。但是,您可以在运行时创建NSArray或类似的

我想在运行时存储数据,我可以有一个链表并在运行时添加,但是由于我对IOS和objective C不熟悉,我们是否有可以添加数据的默认列表(数据是两个Sting和一个整数)

Cocoa提供了一对有序容器,类似于Java的
ArrayList
和C#的
List
。您可以将值添加到
NSMutableArray
,它将随着您添加更多元素而增长
NSArray
是只读的。

您可以使用.plist文件来存储数据。 阅读更多,或者像“从.plist加载数据”一样在谷歌上搜索它。但是,您可以在运行时创建
NSArray
或类似的内容。如果你想深入潜水,你必须阅读,你可以根据需要使用或

NSArray:

NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
NSMutableArray:

NSMutableArray *myArray = [[NSMutableArray alloc] init];
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";
[myArray addObject:aDate];
[myArray addObject:aValue];
[myArray addObject:aString];
NSD词典:

NSDictionary * myDict = [NSDictionary dictionaryWithObjects:aDate, aValue, aString forKeys:firstDate, firstValue, firstString];
NSString *aString = @"a string";
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:aString forKey:firstString];
[myDict setObject:aDate forKey:firstDate];
[myDict setObject:aValue forKey:firstValue];
NSMutableDictionary:

NSDictionary * myDict = [NSDictionary dictionaryWithObjects:aDate, aValue, aString forKeys:firstDate, firstValue, firstString];
NSString *aString = @"a string";
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:aString forKey:firstString];
[myDict setObject:aDate forKey:firstDate];
[myDict setObject:aValue forKey:firstValue];

为数据创建一个具有默认属性的类,并确保它继承NSObject,然后使用NSMUtableArray向列表中添加/删除元素

// in the .h file of your object
@interface MyObject : NSObject {
    NSString* strAttribute1;
    // add more attributes as you want
}

@property (nonatomic, retain) NSString* strAttribute1;

@end

// then in the .m file
// do not forget the #import ""
@implement MyObject
@synthesize strAttribute1;

// override the dealloc to release the retained objects
@end
然后在你的代码中,你想列出这个对象

NSMutableArray* myArray = [[NSMutableArray alloc] init];

// add elements and iterate through them

// do not forgot to free the memory if you are not using ARC
[myArray release];

所以我必须创建一个模态类并将该对象添加到NSMutableArray中,对吗?您需要创建一个类,继承
NSObject
,添加它可能需要的任何属性(例如两个字符串和一个整数),然后将该类的实例添加到您的
NSMutableArray
。为什么要在
[[NSMutableDictionary alloc]中保留额外的属性初始]保留]alloc已被保留。这可能会导致代码泄漏。