Iphone 方法将NSString返回为nil
我有一个构建NSString并正确返回它的方法。 但是,如果我从单独的线程调用相同的方法,它将返回nil,即使我的NSString与return不一致 我在“getPhones”方法中得到request=nil “createSelectPhonesRequest”中的“返回请求”包含有效字符串 “getXmlRow”实际上构建了字符串Iphone 方法将NSString返回为nil,iphone,objective-c,nsstring,nsthread,Iphone,Objective C,Nsstring,Nsthread,我有一个构建NSString并正确返回它的方法。 但是,如果我从单独的线程调用相同的方法,它将返回nil,即使我的NSString与return不一致 我在“getPhones”方法中得到request=nil “createSelectPhonesRequest”中的“返回请求”包含有效字符串 “getXmlRow”实际上构建了字符串 + (void)getUserPhones:(User *)user { NSAutoreleasePool *pool = [[NSAutorelea
+ (void)getUserPhones:(User *)user
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[DataManager getPhones:user];
[pool release];
}
+ (void)getPhones:(User*)user
{
NSString *request = [XMLBuilder createSelectPhonesRequest:user.NickName];
NSString *getAddressesResponce = [TrafficManager sendRequest:request];
}
+(NSString*) createSelectPhonesRequest:(NSString*)nickName
{
NSString *request;
NSMutableArray *attributes;
NSMutableArray *attributesValues;
/*** root ***/
attributes = [NSMutableArray new];
attributesValues = [NSMutableArray new];
//time
[attributes addObject:xmlAt.time];
[attributesValues addObject:[NSDate getXmlFormattedDateFromCurrentDate]];
//action
[attributes addObject:xmlAt.action];
[attributesValues addObject:xmlAV.selectPhones];
request = [XMLBuilder getXmlRow:xmlEl.envelop
attributes:attributes
attributesValues:attributesValues
value:nil
close:YES];
[attributes release];
[attributesValues release];
return request;
}
+(NSString*) getXmlRow:(NSString*)element
attributes:(NSMutableArray*)attributes
attributesValues:(NSMutableArray*)attributesValues
value:(NSString*)value
close:(BOOL)close
{
//open tag
NSString* xmlRow = @"<";
//tag
xmlRow = [xmlRow stringByAppendingString:element];
//attrivutes+values
if (attributes && attributesValues)
for (int i=0; i<[attributes count]; i++)
{
NSString *attribute = (NSString*) [attributes objectAtIndex:i];
NSString *attributeValue = (NSString*) [attributesValues objectAtIndex:i];
xmlRow = [xmlRow stringByAppendingString:@" "];
xmlRow = [xmlRow stringByAppendingString:(attribute)];
xmlRow = [xmlRow stringByAppendingString:@"=\""];
xmlRow = [xmlRow stringByAppendingString:attributeValue];
xmlRow = [xmlRow stringByAppendingString:@"\""];
}
//end tag
xmlRow = [xmlRow stringByAppendingString:@">"];
//value
if (value != nil)
xmlRow = [xmlRow stringByAppendingString:value];
//close tag
if (close)
xmlRow = [xmlRow stringByAppendingString:[self closeElement:element]];
return xmlRow;
}
+(void)getUserPhones:(用户*)用户
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
[DataManager getPhones:用户];
[池释放];
}
+(void)getPhones:(用户*)用户
{
NSString*请求=[XMLBuilder createSelectPhonesRequest:user.昵称];
NSString*GetAddresseSResponse=[TrafficManager发送请求:请求];
}
+(NSString*)createSelectPhonesRequest:(NSString*)昵称
{
NSString*请求;
NSMutableArray*属性;
NSMutableArray*属性值;
/***根***/
属性=[NSMutableArray new];
attributesValues=[NSMutableArray new];
//时间
[属性addObject:xmlAt.time];
[attributesValues添加对象:[NSDate getXmlFormattedDateFromCurrentDate]];
//行动
[属性addObject:xmlAt.action];
[属性值添加对象:xmlAV.selectPhones];
请求=[XMLBuilder getXmlRow:xmlEl.envelope
属性:属性
attributesValues:attributesValues
价值:零
关闭:是];
[属性释放];
[属性值释放];
返回请求;
}
+(NSString*)getXmlRow:(NSString*)元素
属性:(NSMutableArray*)属性
attributesValues:(NSMutableArray*)attributesValues
值:(NSString*)值
关闭:(BOOL)关闭
{
//开放标签
NSString*xmlRow=@'';
//价值观
如果(值!=nil)
xmlRow=[xmlRow stringByAppendingString:value];
//关闭标签
如果(关闭)
xmlRow=[xmlRow stringByAppendingString:[self-closeElement:element]];
返回xmlRow;
}
我认为应该正确初始化字符串,如下所示:
NSString *myString = [NSString stringWithString:@"Hello"];
我怀疑您没有为线程创建自动释放池。每个线程都必须有自己的自动释放池 这没用。我仍然在调用method.NSString*myString=[[NSString alloc]initWithString:@“Hello”]时得到nil//然后你的代码返回[myString autoRelease];得到了同样的效果。可能是因为我的方法是静态的,所以出现了一些问题。我确实有自动释放池。可能是自动释放造成了问题。但是我尝试分配字符串,但得到了相同的效果。您的XML对线程调用有效吗?您的新编辑与您之前的简单示例几乎没有相似之处,这让我想到了自动释放池。是的。我没有发布所有createSelectPhonesRequest,因为没有必要。里面所有的线都是一样的。你注意到我所有的方法都是静态的吗?使用它对你有多大用处这对我来说应该也很好,但事实并非如此。只有当我在没有线程的情况下同步运行它时,它才会运行。