【Object-C】处理对象:description 方法、isEqual方法

description 方法是NSObject 的一个实例方法,所有继承NSObject基类的类都具有该方法。用于“描述自我”,当执行该方法师,系统将输出该对象的自我描述信息。
NSObject 类提供的 description 方法总是返回<类名:16进制首地址>,如果需要更详细的描述类,需要自己定义description 方法。

O-c中判断两个变量是否相等由两种方法:
1、利用  == 运算符
2、利用isEqual 方法
//
//  main.m
//  ISEQUAL
//
//  Created by mac on 14-11-28.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
   
 @autoreleasepool {
       
 // insert code here...
        NSLog(
@"Hello, World!");
       
       
 int it = 65;
       
 float fl = 65.0f;
       
 // 将输出1代表真
        NSLog(
@"6565.0f是否相等?: %d" , (it == fl));
       
 char ch = 'A';
       
 // 将输出1代表真
        NSLog(
@"65'A'是否相等?%d" , (it == ch));
        NSString* str1 = [NSString stringWithFormat:
@"hello"];
        NSString* str2 = [NSString stringWithFormat:
@"hello"];
        NSString* str3 = [NSString stringWithFormat:
@"Hi"];
       
 // 将输出0代表假
        NSLog(
@"str1str2是否相等?%d", (str1 == str2));
       
 // 将输出1代表真
        NSLog(
@"str1是否isEqual str2%d", [str1 isEqual:str2]);
       
 // 由于NSDateNSString类没有继承关系,

        NSLog(
@"str1 is equal str3 ? %i",[str1 isEqual:str3]);
       
    }
   
 return 0;
}

==>>
2014-11-28 19:12:29.006 ISEQUAL[2359:303] Hello, World!
2014-11-28 19:12:29.007 ISEQUAL[2359:303] 65
65.0f是否相等?: 1
2014-11-28 19:12:29.008 ISEQUAL[2359:303] 65
'A'是否相等?1
2014-11-28 19:12:29.010 ISEQUAL[2359:303] str1
str2是否相等?0
2014-11-28 19:12:29.010 ISEQUAL[2359:303] str1
是否isEqual str21
2014-11-28 19:12:29.011 ISEQUAL[2359:303] str1 is equal str3 ? 0
Program ended with exit code: 0

posted @ 2014-11-30 11:37  shujucn  阅读(184)  评论(0编辑  收藏  举报