數值型賦值給布爾型,1.NSString 包含著許許多多的特性,比如可以直接轉化為數字類型(整型,浮點型)
1 NSString *test = @"this is my name";
2 if(NSOrderedAscending== [test compare:@"this is my name 2"]) {
3 NSLog(@"升值");
4 }
5 NSString *floatValueString = @"12";
6
7 NSLog(@"%d floatValue is%f ",[test length],[floatValueString floatValue]);
2.如何用NSLog輸出BOOL類型的值。
BOOL isBool = YES;
NSLog(@"%@",isBool==YES? @"YES":@"NO");
3,可以講整型賦值給BOOL類型,輸出的結果為“
數值型賦值給布爾型,14”
BOOL canAcceptDigital = 14;
NSLog(@"數值型賦值給布爾型,%d",canAcceptDigital);
4, 在objective-c中,YES代表1,而NO代表0
1 if (2) {
2 NSLog(@"2可以執行被執行");
3 NSLog(@"YES is %d",YES);
4 NSLog(@"NO is %d",NO);
5 }
結果輸出如下:
2011-12-27 21:12:04.763 lookbook[791:207] 2可以執行被執行
2011-12-27 21:12:04.773 lookbook[791:207]
YES is 12011-12-27 21:12:04.778 lookbook[791:207]
NO is 0
5.利用NSLog 中的%@,可以輸出對象中的description函數中的定義。
標頭檔:person.h
1 #import <Foundation/Foundation.h>
2
3
4 @interface person : NSObject {
5
6 }
7 - (NSString *)description;
8 @end
實現檔案person.m
#import "person.h"
@implementation person
- (NSString *)description
{
return @"my name is description";
}
@end
在 viewDidLoad中添加如下方法:
- (void)viewDidLoad {
[super viewDidLoad];
person *d = [[person alloc]init ];
NSLog(@"person's description is %@",d);
}
運行之後,結果如下:
person's description is my name is description
下一章《物件導向編程基礎知識》待續。 敬請期待!