NSString 中的 NSMutableArray

发布于 2024-11-03 12:09:56 字数 1681 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-11-10 12:09:56

最简单的方法是创建一个非常简单的模型类:

"Person.h"

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}

@property (readwrite, copy) NSString *firstName;
@property (readwrite, copy) NSString *lastName;

@end

"Person.m"

#import "Person.h"

@implementation Person

@synthesize firstName, lastName;

- (void)dealloc
{
    [firstName release];
    firstName = nil;

    [lastName release];
    lastName = nil;

    [super dealloc];
}

@end

这将允许您使用 addObject: 因为它在技术上是一个对象,您只能在其中存储多条数据。示例如下:

//...
Person *p1 = [[Person alloc] init];
[p1 setFirstName:@"First Name"];
[p2 setLastName:@"Last Name"];

[personArray addObject:p1];
[p1 release];
//...

如果您不熟悉 @property@synthesize 我会推荐这篇文章解释两者


为了响应编辑,您将能够使用如下所示的方式获取值:

Person *peep = [personArray objectAtIndex:i];
NSString *firstName = [peep firstName]; //or you could use peep.firstName;
NSString *lastName = [peep lastName];

The easiest way to do this would be to crank out a very simple model class:

"Person.h"

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}

@property (readwrite, copy) NSString *firstName;
@property (readwrite, copy) NSString *lastName;

@end

"Person.m"

#import "Person.h"

@implementation Person

@synthesize firstName, lastName;

- (void)dealloc
{
    [firstName release];
    firstName = nil;

    [lastName release];
    lastName = nil;

    [super dealloc];
}

@end

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:

//...
Person *p1 = [[Person alloc] init];
[p1 setFirstName:@"First Name"];
[p2 setLastName:@"Last Name"];

[personArray addObject:p1];
[p1 release];
//...

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:

Person *peep = [personArray objectAtIndex:i];
NSString *firstName = [peep firstName]; //or you could use peep.firstName;
NSString *lastName = [peep lastName];
守望孤独 2024-11-10 12:09:56

试试这个:

NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:
                        @"Bill", @"Firstname", 
                        @"Gates", @"Lastname", nil];
NSMutableArray *storage = [[[NSMutableArray alloc] init] autorelease];
[storage addObject:person];
[person release];

Try this:

NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:
                        @"Bill", @"Firstname", 
                        @"Gates", @"Lastname", nil];
NSMutableArray *storage = [[[NSMutableArray alloc] init] autorelease];
[storage addObject:person];
[person release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文