关于oc定义的一些问题

#import "Fraction.h"

@implementation Fraction

@synthesize numerator,denominator;

-(void) print
{
    NSLog(@"%i/%i",numerator,denominator);
}
@end



#import "Fraction.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        Fraction *a=[[Fraction alloc] init];
        Fraction *b;
        [a print];
        [b print];
        NSLog(@"%i/%i",a.numerator,a.denominator);
        NSLog(@"%i/%i",[b numerator],[b denominator]);
        NSLog(@"%i",a);
        NSLog(@"%i",b);
        if (!a) {
            NSLog(@"123");
        }
        if (!b) {
            NSLog(@"456");
        }
        a.numerator=1;
        a.denominator=3;
        b.numerator=2;
        b.denominator=5;
        NSLog(@"%i/%i",a.numerator,a.denominator);
        NSLog(@"%i/%i",[b numerator],[b denominator]);
        
    }
    return 0;
}
 

2015-04-26 10:05:05.981 Try[821:303] 0/0
2015-04-26 10:05:05.984 Try[821:303] 0/0
2015-04-26 10:05:05.985 Try[821:303] 0/0
2015-04-26 10:05:05.987 Try[821:303] 1087952
2015-04-26 10:05:05.987 Try[821:303] 0
2015-04-26 10:05:05.988 Try[821:303] 456
2015-04-26 10:05:05.989 Try[821:303] 1/3
2015-04-26 10:05:05.990 Try[821:303] 0/0

a正常初始化 实例变量为0/0 指针指向任意地址  b由于编译器初始化为nil 即空(nil == 0) 指针指向地址0x00000000

[a print]正常输出 [b print]由于对象为空 没有输出

a的两条NSLog语句分别输出实例变量和地址 b分别输出nil和0

后面b的判断也确实为0 对其设值也是一样为nil 因为没有正常初始化

最后简单类型由于编译器初始化都为0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值