zoukankan      html  css  js  c++  java
  • stringWithFormat 和 initWithFormat 区别

    1、initWithFormat是实例办法

    只能经由过程 NSString* str = [[NSString alloc] initWithFormat:@"%@",@"Hello World"] 调用,然则必须手动release来开释内存资料

    2、stringWithFormat是类办法

    可以直接用 NSString* str = [NSString stringWithFormat:@"%@",@"Hello World"] 调用,内存经管上是autorelease的,不消手动显式release


    别的国外有个贴子对此有专门评论辩论

    并且提出了一个常见错误:

    label.text = [[NSString alloc] initWithFormat:@"%@",@"abc"];

    最后在dealloc中将label给release掉

    然则仍然会产生内存泄漏!

    原因在于:用label.text = ...时,实际是隐式调用的label的setText办法,这会retain label内部的字符串变量text(哪怕这个字符串的内容跟传进来的字符串内容雷同,但体系仍然当成二个不合的字符串对象),所以最后release label时,实际上只开释了label内部的text字符串,然则最初用initWithFormat生成的字符串并未开释,终极造成了泄漏。

    解决办法有二个:

    1、

    NSString * str = [[NSString alloc] initWithFormat:@"%@",@"abc"];

    label.text = str;

    [str release]

    最后在dealloc中再[label release]

    2、

    label.text = [NSString stringWithFormat:@"%@",@"abc"];

    然后剩下的工作交给NSAutoreleasePool

    最后,若是你不断定你的代码是否有内存泄漏题目,可以用Xcode中的Build-->Build And Analyze 做初步的搜检.


    前几天,同事提到initWithString和initWithFormat的区别问题,觉得很有意思,决定研究下,现把成果和大家分享。

    下面是测试代码:
    NSString * str =[[NSString alloc] initWithString:@"this is from initWithString function"];
    NSLog(@"this is from [[NSString alloc] initWithString] m_addr is %ld retainCount is %i", str, [str retainCount]);
    [str release];
    [str release];
    [str release];
    NSLog(@"this is from [[NSString alloc] initWithString] m_addr is %ld retainCount is %i", str, [str retainCount]);

    str = [[NSString alloc] initWithFormat:@"this is from initWithFormat function"];
    NSLog(@"this is from [[NSString alloc] initWithFormat] m_addr is %ld retainCount is %d", str, [str retainCount]);

    下面是LOG的结果:
    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithFormat] m_addr is 82076688 retainCount is 1

    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithFormat] m_addr is 78748112 retainCount is 1

    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithString] m_addr is 12356 retainCount is 2147483647
    this is from [[NSString alloc] initWithFormat] m_addr is 78777072 retainCount is 1

    我将上面这段测试代码调用了三次,得到以上的LOG结果。顺便得出这么几条结论。对于不对,请大家鉴定。
    1.从两个变量的地址看,两个变量的地址差据较大。前者的地址非常靠前。
    2.从release看,前者无论被release多少次,都不会被释放,而且的值不变,而后者只要release一次,变量即消亡。
    3.前者的releaseCount= NSIntegerMax,而NSIntegerMax ==INT_MAX ,而 UINT_MAX== (INT_MAX * 2U + 1U)。
    U是指无符号整型,而我们默认的int和NSInteger是有符号的,在32位系统中,NSIntegerMax=0X7FFFFFFF,对其乘以2U,即一次向左挪一位。结果为0XFFFFFFFE,再加1U为0XFFFFFFFF,即无符号的最大值。
    然后根据苹果官方对于retainCount方法的描述:
    retainCount
    Returns the receiver’s reference count. (required)

    - (NSUInteger)retainCount
    Return Value
    The receiver’s reference count.

    Discussion
    You might override this method in a class to implement your own reference-counting scheme. For objects that never get released (that is, their release method does nothing), this method should return UINT_MAX, as defined in <limits.h>.

    小生窃以为,此处的UINT_MAX和NSIntegerMax是一样的,都是表示所在类型的最大值。所以,initWithString这个方法初始化后的对象是不可能被release的或者说,它的release方法啥也不干。

    验证了上面的分析2.因为他不可能被release的或者说,它的release方法啥也不干,所以我们调用无数次release都没有起到预先想想的作用。

    为什么会导致这样的情况呢?

    我们再次把目光转向地址。眼尖的同学可能会看到initWithString申请的地址每次都是一样的,而initWithFormat的地址每次都不一样,这个说明什么?

    说明initWithString的地址是静态的,而initWithFormat是动态的。为什么前者是静态的,而后者是动态的?

    结合上面关于retainCount的分析,小生窃以为initWithString的地址申请是在编译是进行的,这样才能说明为什么它的地址空间如此靠前。只有在编译是进行的,他才是静态的。

    对于initWithString生成的对象,对其进行dealloc时,程序会报错(这里就不贴LOG了)。而后者initWithFormat不会报错。这进一步验证了initWithString生成的是静态对象,而initWithFormat是动态的。



    结论:initWithString生成的对象是在编译是申请地址空间,而且在程序中不能释放,不建议使用。(当然也有可能在某种情况下会使用到这个方法,在此不多加讨论)。

    在ObjectiveC中NSString中有一个 stringWithFormat:方法

    常见的输出方式:
    NSString *height;
    height = [NSString stringWithFormat:@"Your height is %d feet, %d inches.",5,11];
    NSLog(@"%@",height);

    输出结果:
    2013-04-12 10:30:47.744 String[2161:303] Your height is 5 feet, 11 inches.


    输出多个字符的方式(以两个字符为例):

    NSString *str;
    NSString *str1 = @"123";
    NSString *str2 = @"465";
    str = [NSString stringWithFormat:@"%@,%@",str1, str2];
    NSLog(@"%@",str);

    输出结果:
    2013-04-12 10:31:48.213 String[2171:303] 123,465

    下面这种方式是错误的:

    str = [NSString stringWithFormat:@"123",@"456"];
    NSLog(@"%@",str);

    关于stringWithFormat:
    输出结果:
    2013-04-12 10:35:34.043 String[2209:303] 123
    @“456” 不会被输出;

    以上代码可以改写成:
    str = [NSString stringWithFormat:@"%@,%@",@"123",@"456"];
    NSLog(@"%@",str);

    输出结果:
    2013-04-12 10:42:39.541 String[2229:303] 123,456


    我们经常会初始化一些string使用NSString的stringWithString函数
    但使用时发现了一个stringWithString的问题,如图

    当参数是nil时,stringWithString会crash,所以使用时必须验证参数不是nil
    相比较stringWithFormat就不会crash但返回的str也不是nil而是@"(null)"
    所以再做此类操作时事先要判断参数

  • 相关阅读:
    结构体和指针
    冒泡排序、指针、函数
    指针学习笔记
    C++学习笔记
    背包问题
    零钱兑换2【DP】
    零钱兑换1【DP】
    执行hadoop jar命令时报Not a valid JAR的原因分析和解决方案
    斐波那契数列
    具有n个结点的二叉树有多少种形态
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4444842.html
Copyright © 2011-2022 走看看