NSString 中的 NSMutableArray
在 10 年没有编程(当时使用 Delphi)之后,我开始学习 Objective-C。我承认我遇到了很多问题,因为这种方法非常不同。
我想制作一个简单的表格,其中每行由几个组成 NSString 变量。举个例子:
ID Firstname Lastname
01 FN1 LN1
02 FN2 LN2
我的表:
NSString * FirstName, * LastName;
NSMutableArray * person = [NSMutableArray arrayWithObjects: firtsname, lastname, nil];
问题是我不能用2个NSString添加一行,例如:
FirstName = @ "FN1";
LastName = @ "LN1";
[person AddObject: FirstName, LastName];
FirstName = @ "FN2";
LastName = @ "LN2";
[person AddObject: FirstName, LastName];
//etc. ...
我知道AddObject只能添加1个对象,但它是为了解释我的例子的概念。
如何像我的示例一样同时添加 2 个对象?
那么,我如何管理/检索对象 NString "1" (firstName) & NString "2" (lastName) 按索引 (i);
我承认我找不到发展的逻辑,你的回答将会非常有帮助。
非常感谢您的回复。
编辑:
我有一个奇怪的问题。我的 .m :
Person *pers = [[Person alloc]init];
NSMutableArray *myarray = [[NSMutableArray alloc] init];
[pers setFirstName:@"FN 1"]; // = pers.firstName;
[pers setLastName:@"LN 1"]; // = pers.lastName;
[myarray addObject:pers];
[pers setFirstName:@"FN 2"];
[pers setLastName:@"LN 2"];
[myarray addObject:pers];
[pers release];
NSLog(@"count: %d", [myarray count]);
for(int i = 0; i < [myarray count]; i++)
{
pers = [myarray objectAtIndex:i];
NSLog(@"%d %@ %@ %@", i, pers, pers.firstName, pers.lastName);
}
[myarray release];
我的 NSLog 是: 2011-04-27 19:07:57.582 临时[11724:903] 计数:2
2011-04-27 19:07:57.584 临时[11724:903] 0 FN 2 LN 2
2011-04-27 19:07:57.585 temp[11724:903] 1 FN 2 LN 2
正如你所看到的,我有 FN 2 LN 2 两次。对象“pers”有两倍相同的值
正常吗?
After 10 years without programming (in Delphi at the time), I began learning
Objective-C. I confess I have met lots of problems as this approach is very different.
I want to make a simple table in which each line consists of several
NSString variables. An example:
ID Firstname Lastname
01 FN1 LN1
02 FN2 LN2
My table:
NSString * FirstName, * LastName;
NSMutableArray * person = [NSMutableArray arrayWithObjects: firtsname, lastname, nil];
The problem is that I can not add a line with the 2 NSString, for example:
FirstName = @ "FN1";
LastName = @ "LN1";
[person AddObject: FirstName, LastName];
FirstName = @ "FN2";
LastName = @ "LN2";
[person AddObject: FirstName, LastName];
//etc. ...
I know that AddObject can only add 1 object but it is to explain the concept of my example.
How can I add 2 objects at the same time like in my example?
then, how can i manage/retrieve objects NString "1" (firstName) & NString "2" (lastName) by the index (i);
I confess I can not find the logic of development and your responses would be very helpful.
Thank you very much for your reply.
EDIT :
I have a strange problem. My .m :
Person *pers = [[Person alloc]init];
NSMutableArray *myarray = [[NSMutableArray alloc] init];
[pers setFirstName:@"FN 1"]; // = pers.firstName;
[pers setLastName:@"LN 1"]; // = pers.lastName;
[myarray addObject:pers];
[pers setFirstName:@"FN 2"];
[pers setLastName:@"LN 2"];
[myarray addObject:pers];
[pers release];
NSLog(@"count: %d", [myarray count]);
for(int i = 0; i < [myarray count]; i++)
{
pers = [myarray objectAtIndex:i];
NSLog(@"%d %@ %@ %@", i, pers, pers.firstName, pers.lastName);
}
[myarray release];
and my NSLog is :
2011-04-27 19:07:57.582 temp[11724:903] count: 2
2011-04-27 19:07:57.584 temp[11724:903] 0 FN 2 LN 2
2011-04-27 19:07:57.585 temp[11724:903] 1 FN 2 LN 2
As you can see, i have FN 2 LN 2 twice. Object "pers" have twice the same value
Normal ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是创建一个非常简单的模型类:
"Person.h"
"Person.m"
这将允许您使用
addObject:
因为它在技术上是一个对象,您只能在其中存储多条数据。示例如下:如果您不熟悉
@property
或@synthesize
我会推荐这篇文章解释两者。为了响应编辑,您将能够使用如下所示的方式获取值:
The easiest way to do this would be to crank out a very simple model class:
"Person.h"
"Person.m"
This would allow you to use
addObject:
since it would technically be one object, you're just able to store multiple pieces of data in it. An example follows:If you aren't familiar with
@property
or@synthesize
I would recommend this article explaining both.In response to the edit, you would be able to get at the values using something like the following:
试试这个:
Try this: