Objective c nsmutablearray元素到nsstring
我想检索在Objective c nsmutablearray元素到nsstring,objective-c,cocoa,nsstring,nsmutablearray,nscombobox,Objective C,Cocoa,Nsstring,Nsmutablearray,Nscombobox,我想检索在NSMutableArray中解析的元素,并将它们存储到NSString变量中,然后将它们存储在NSMutableArray中,作为NSString(因为我想在NSComboBox中显示内容)。我试过了,但没用。你能解决这个问题吗,我无法解决: //--this is the parsing code : - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespace
NSMutableArray
中解析的元素,并将它们存储到NSString
变量中,然后将它们存储在NSMutableArray
中,作为NSString
(因为我想在NSComboBox
中显示内容)。我试过了,但没用。你能解决这个问题吗,我无法解决:
//--this is the parsing code :
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"user"]) {
NSLog(@"user element found – create a new instance of User class...");
if(currentElementValue == nil)
currentElementValue = [NSMutableString string];
else
[currentElementValue setString:@""];
}
else {
currentElementValue = nil;
}
user = [[User alloc] init];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
if (currentElementValue)
{
currentElementValue = nil;
}
}
NSLog(@"Processing value for : %@", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"users"]) {
// We reached the end of the XML document
return;
NSLog(@"QUIT");
}
if ([elementName isEqualToString:@"userName"]) {
[[self user] setUserName:currentElementValue];
NSLog(@"final step for value: %@", user.userName);
NSLog(@"currentElementName content : %@", currentElementValue);
[currentElementValue release];
NSLog(@"release : %@", currentElementValue);
currentElementValue = nil;
NSLog(@"release : %@", currentElementValue);
}
if ([elementName isEqualToString:@"firstName"]) {
[[self user] setFirstName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:@"lastName"]) {
[[self user] setLastName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:@"user"]) {
NSLog(@"\n user=%@ \n",user);
[users addObject:user];
NSLog(@"userName test : %@", users);
[user release];
user = nil;
}
}
-(BOOL)parseDocumentWithData:(NSData *)data {
if (data == nil)
return NO;
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:NO];
BOOL ok = [xmlparser parse];
if (ok == NO)
NSLog(@"error");
else
NSLog(@"OK");
[xmlparser release];
return ok;
}
// this is the xml file :
<users>
<user>
<userName>mspeller</userName>
<firstName>Mike</firstName>
<lastName>Speller</lastName>
</user>
<user>
<userName>mgdan</userName>
<firstName>Mila</firstName>
<lastName>Gdan</lastName>
</user>
</users>
//-------
NSMutableArray *tabletest= [[NSMutableArray alloc] init];
NSMutableString * result = [[NSMutableString alloc] init];
int i;
for(i=0; i < [users count]; i++){
[result appendString:[NSString stringWithFormat:@"%@",[[users objectAtIndex:i] valueForKey:@"userName"]] ];
NSLog(@"result==%@",result);
[tabletest addObject:result];
}
/--这是解析代码:
-(void)解析器:(NSXMLParser*)解析器
didStartElement:(NSString*)元素名称
namespaceURI:(NSString*)namespaceURI
qualifiedName:(NSString*)qualifiedName
属性:(NSDictionary*)属性指令{
if([elementName IsequalString:@“user”]){
NSLog(@“找到用户元素–创建用户类的新实例…”);
如果(currentElementValue==nil)
currentElementValue=[NSMutableString];
其他的
[currentElementValue设置字符串:@”“;
}
否则{
currentElementValue=零;
}
user=[[user alloc]init];
}
-(void)解析器:(NSXMLParser*)解析器查找字符:(NSString*)字符串{
如果(!currentElementValue){
//使用值初始化临时字符串
currentElementValue=[[NSMutableString alloc]initWithString:string];
}否则{
//将值附加到特殊字符串
[currentElementValue appendString:string];
if(currentElementValue)
{
currentElementValue=零;
}
}
NSLog(@“处理值:%@”,字符串);
}
-(void)解析器:(NSXMLParser*)解析器
DiEndElement:(NSString*)elementName
namespaceURI:(NSString*)namespaceURI
限定名称:(NSString*)qName{
if([elementName IsequalString:@“users”]){
//我们到达了XML文档的末尾
返回;
NSLog(@“退出”);
}
如果([elementName IsequalString:@“userName”]){
[[self user]setUserName:currentElementValue];
NSLog(@“值的最后一步:%@”,user.userName);
NSLog(@“currentElementName内容:%@”,currentElementValue);
[当前元素值释放];
NSLog(@“release:%@”,currentElementValue);
currentElementValue=零;
NSLog(@“release:%@”,currentElementValue);
}
if([elementName IsequalString:@“firstName”]){
[[self-user]setFirstName:currentElementValue];
[当前元素值释放];
currentElementValue=零;
}
if([elementName IsequalString:@“lastName”]){
[[self-user]setLastName:currentElementValue];
[当前元素值释放];
currentElementValue=零;
}
if([elementName IsequalString:@“user”]){
NSLog(@“\n user=%@\n”,user);
[用户添加对象:用户];
NSLog(@“用户名测试:%@”,用户);
[用户发布];
用户=零;
}
}
-(BOOL)parseDocumentWithData:(NSData*)数据{
如果(数据==nil)
返回否;
NSXMLParser*xmlparser=[[NSXMLParser alloc]initWithData:data];
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:否];
BOOL ok=[xmlparser parse];
如果(确定==否)
NSLog(@“错误”);
其他的
NSLog(@“正常”);
[xmlparser release];
返回ok;
}
//这是xml文件:
姆斯佩勒
迈克
拼写者
姆格丹
米拉
格丹
//-------
NSMutableArray*tabletest=[[NSMutableArray alloc]init];
NSMutableString*结果=[[NSMutableString alloc]init];
int i;
对于(i=0;i<[用户计数];i++){
[结果appendString:[NSString stringWithFormat:@“%@”,[[users objectAtIndex:i]valueForKey:@“userName”]];
NSLog(@“结果==%@”,结果);
[表测试添加对象:结果];
}
根据您在评论部分的链接,我认为您访问“用户名”属性的方式不对。您正试图访问它,因为用户包含NSDictionary
对象。据我所知,您正在将User
对象添加到NSMutableArray
尝试以下内容(我冒昧地对代码进行了一些美化):
如果我完全误解了您的设计,请纠正我。我不明白您的意图,但您的代码目前所做的是将相同的字符串
[user count]
time添加到数组tabletest
,如下所示:
该行:
[result appendString:[NSString stringWithFormat:@"%@",[[users objectAtIndex:i] valueForKey:@"userName"]] ];
[tabletest addObject:result];
将每个[[users objectAtIndex:i]valueForKey:@“userName”]
一起追加的结果累加到result
中-循环的每次迭代都会将下一项添加到result
的末尾
该行:
[result appendString:[NSString stringWithFormat:@"%@",[[users objectAtIndex:i] valueForKey:@"userName"]] ];
[tabletest addObject:result];
将result
引用的对象添加到数组中。这在每次迭代中执行一次,因此数组最终会引用同一对象。将对可变字符串的引用放入数组不会放置其当前值的副本,而只是对字符串的引用-对字符串进行变异,并且通过存储在数组中的引用可以看到变异
因此,代码的最终结果是对同一可变字符串的[users count]
引用数组,该字符串是所有[[users objectAtIndex:i]valueForKey:@“userName”]
值的串联
你的意图是什么
如果您试图创建[[users objectAtIndex:i]valueForKey:@“userName”]
的字符串表示数组,请将代码更改为:
NSMutableArray *tabletest= [[NSMutableArray alloc] init];
for(int i = 0; i < [users count]; i++)
{
// create a string representation of userName
NSString *result = [NSString stringWithFormat:@"%@",[[users objectAtIndex:i] objectForKey:@"userName"]];
// add the string to the array
[tabletest addObject:result];
}
NSMutableArray*tabletest=[[NSMutableArray alloc]init];
对于(int i=0;i<[用户计数];i++)
{
//创建用户名的字符串表示形式
NSString*result=[NSString stringWithFormat:@“%@”,[[users objectAtIndex:i]objectForKey:@“userName”];
//将字符串添加到数组中
[表测试添加对象:结果];
}
但也许你的意图是别的?什么是用户?字典?array?users是nsmutablearray,它包含从xml文件(用户名、FirsName和lastName)解析的数据。您可以将代码发布到您从xml文件解析的位置吗