方法将NSString返回为nil

时间:2011-11-21 05:49:42

标签: iphone objective-c nsstring nsthread

我有构建NSString并正确返回它的方法。 但是,如果我从单独的线程调用相同的方法,它返回nil,即使我的NSString与返回符号不一致。

我在“getPhones”方法中得到request = nil。

“createSelectPhonesRequest”中的“return request”包含有效字符串。

“getXmlRow”实际构建字符串。

+ (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;
}

2 个答案:

答案 0 :(得分:1)

我怀疑你没有为线程创建自动释放池。每个线程都必须拥有自己的自动释放池。

答案 1 :(得分:0)

我认为你应该正确地初始化字符串,如

NSString *myString = [NSString stringWithString:@"Hello"];