字符串分配和初始化

时间:2013-01-22 07:59:00

标签: iphone ios objective-c xcode

  

可能重复:
  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  = [[NSStrin@"Date and Time field is required."];

}    

如果我使用 Nsstring * str_Message = nil; '

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 * str会产生什么影响呢? Nsstring * str = nil

1 个答案:

答案 0 :(得分:2)

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

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