欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Objective--C语言字符串方法命名 博客分类: XCode || Objective-C  

程序员文章站 2024-02-14 21:15:46
...

1:在JAVA语言里,你肯定定义一个返回值是String的函数(方法),肯定不要加上指针*标示。但是Objective--C就需要。

2:代码

#import <Foundation/Foundation.h>


// returns No if the two integers have the same
//value ,YES otherwise
BOOL areIntsDifferent(int thing1,int thing2)
{
    if (thing1 == thing2){
        return (NO);
    } else {
        return (YES);
    }

}//areIntsDifferent


NSString *boolString(BOOL yesNo)
{
    if(yesNo == NO){
        return (@"NO");
    } else {
        return (@"YES");
    }

}// boolString

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...

        BOOL areTheyDifferent;
        
        areTheyDifferent = areIntsDifferent(5, 5);
        
        NSLog(@"are %d and %d different? %@",5,5,boolString(areTheyDifferent));
        
        areTheyDifferent = areIntsDifferent(23, 42);
        
        NSLog(@"are %d and %d different ? %@",23,42,boolString(areTheyDifferent));
    }
    return 0;
}

 

3:扩展

        NSLog(@"are %d and %d different ? %@",23,42,boolString(areTheyDifferent));

 在代码中%@是NSLog()的专用格式说明符,它支持将NSString的值插入到NSLog中。