2013-01-22 59 views
-1

可能重复:
iPhone Dev - NSString Creation字符串分配和初始化

我使用

NSString *str_Message; 
if ([txt_NoOfPersons.text length] == 0){ 
    bln_Validate = FALSE; 
    str_Message = [[NSString alloc]initWithString:@"Number of Persons field is required."]; 
} 
else if ([txt_NoOfPersons.text intValue] == 0) 
{ 
    bln_Validate = FALSE; 
    str_Message = [[NSString alloc]initWithString:@"Please enter your guest count."];} 
} 
if ([txt_DateAndTime.text length] == 0) 
{ 
    bln_Validate = FALSE; 
    str_Message = [[[email protected]"Date and Time field is required."]; 

}  

insted的的,如果我使用的NSString * str_Message =零;'

if ([txt_NoOfPersons.text length] == 0) 
{ 
    bln_Validate = FALSE; 
    str_Message = @"Number of Persons field is required."; 
} 
else if ([txt_NoOfPersons.text intValue] == 0) 
{ 
    bln_Validate = FALSE; 
    str_Message = @"Please enter your guest count."; 

} 

那么会是怎样的效果是什么的NSString *海峡之间的差异; Nsstring * str = nil

+0

我想知道它们之间的区别是什么只设置它为零删除垃圾值 – saurabh

回答

2

当使用ARC时,NSString *str;NSString *str = nil;之间没有区别,所有指针在初始化时保证为零。

当您不使用ARC时,指针可能指向垃圾值。

+0

我没有使用ARC。当您为不同的条件为相同的字符串分配不同的值时会发生什么.. – saurabh

+0

哪一个适合STR = @ “”;或者str = nil;如果我必须将str值传递给Web服务 – saurabh