NSString 中的 NSMutableArray [英] NSMutableArray within NSString
问题描述
10 年没有编程(当时在 Delphi 中),我开始学习目标-C.我承认我遇到了很多问题,因为这种方法非常不同.
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.
我想制作一个简单的表格,其中每一行由几个NSString 变量.一个例子:
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
我的桌子:
NSString * FirstName, * LastName;
NSMutableArray * person = [NSMutableArray arrayWithObjects: firtsname, lastname, nil];
问题是我不能用2个NSString添加一行,例如:
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. ...
我知道 AddObject 只能添加 1 个对象,但它是为了解释我的示例的概念.
I know that AddObject can only add 1 object but it is to explain the concept of my example.
如何像示例中那样同时添加 2 个对象?
How can I add 2 objects at the same time like in my example?
那么,我如何管理/检索对象 NString "1" (firstName) &NString "2" (lastName) 由索引 (i);
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.
非常感谢您的回复.
我有一个奇怪的问题.我的.m :
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];
我的 NSLog 是:2011-04-27 19:07:57.582 temp[11724:903] 计数:2
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.584 temp[11724:903] 0 FN 2 LN 2
2011-04-27 19:07:57.585 temp[11724:903] 1 FN 2 LN 2
2011-04-27 19:07:57.585 temp[11724:903] 1 FN 2 LN 2
如您所见,我有两次 FN 2 LN 2.对象pers"具有两倍相同的值
As you can see, i have FN 2 LN 2 twice. Object "pers" have twice the same value
正常?
推荐答案
最简单的方法是创建一个非常简单的模型类:
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
这将允许您使用 addObject:
因为它在技术上是一个对象,您只能在其中存储多条数据.一个例子如下:
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];
//...
如果您不熟悉 @property
或 @synthesize
我会推荐这篇解释两者的文章.
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];
这篇关于NSString 中的 NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!