【问题标题】:String Allocation and initialization [duplicate]字符串分配和初始化
【发布时间】:2013-01-22 07:59:00
【问题描述】:

可能重复:
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

【问题讨论】:

  • 我想知道它们之间有什么区别,只将它设置为 nil 删除垃圾值

标签: iphone ios objective-c xcode


【解决方案1】:

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

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

【讨论】:

  • 我没有使用 ARC。当你为不同的条件为相同的字符串分配不同的值时会发生什么..
  • 哪个1合适 str=@"";或 str=nil;如果我必须将 str 值传递给 Web 服务
猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多