• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

初学Objective-C

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

闲来无聊看了下iOS5编程,第一章是一个Hello World

不过由于书上是以xcode 4.2为基础讲的,和4.3区别很大,我也懒得去研究了。

看第二章吧,讲讲Objectve-C的一些基础知识,看着也没啥吃力的,随便翻了下。

以一个RandomPossession为例子讲的OC的类的创建和一些操作,比如传递消息什么的,和函数调用类似。

 

Possession.h

#import <Foundation/Foundation.h>

@interface Possession : NSObject
{
    NSString *possessionName;
    NSString *serialNumber;
    int valueInDollars;
    NSDate *dateCreated;
}
- (void)setPossessionName:(NSString *)str;
- (NSString *)possessionName;

- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;

- (void)setValueInDollars:(int)i;
- (int)valueInDollars;

- (NSDate *)dateCreated;

- (id)initWithPossessionName:(NSString *)name
              valueInDollars:(int)value
                serialNumber:(NSString *)sNumber;
+ (id)randomPossession;
@end

 

Possession.m

#import "Possession.h"

@implementation Possession

- (void)setPossessionName:(NSString *)str
{
    possessionName = str;
}
- (NSString *)possessionName
{
    return possessionName;
}

- (void)setSerialNumber:(NSString *)str
{
    serialNumber = str;
}
- (NSString *)serialNumber
{
    return serialNumber;
}

- (void)setValueInDollars:(int)i
{
    valueInDollars = i;
}
- (int)valueInDollars
{
    return valueInDollars;
}

- (NSDate *)dateCreated
{
    return dateCreated;
}

- (NSString *)description
{
    NSString *descriptionString = 
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d , recorded on %@",
     possessionName,
     serialNumber,
     valueInDollars,
     dateCreated];
    return descriptionString;
}

- (id)initWithPossessionName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
    self = [super init];
    if(self){
        [self setPossessionName:name];
        [self setSerialNumber:sNumber];
        [self setValueInDollars:value];
        
        dateCreated = [[NSDate alloc] init];
    }
    return self;
}

+ (id)randomPossession
{
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy" , @"Rusty" , @"Shiny",nil];
    
    NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",@"Spork" ,@"Mac" , nil];
    
    int adjectiveIndex = rand() % [randomAdjectiveList count];
    int nounIndex = rand() % [randomNounList count];
    
    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            [randomAdjectiveList objectAtIndex:adjectiveIndex],
                            [randomNounList objectAtIndex:nounIndex]];
    int randomValue = rand() % 100;
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10,
                                    'A' + rand() % 26,
                                    '0' + rand() % 10];
    
    Possession *newPossession = 
    [[self alloc] initWithPossessionName:randomName valueInDollars:randomValue serialNumber:randomSerialNumber];
    
    return newPossession;
}
@end

 main.m

#import <Foundation/Foundation.h>
#import "Possession.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSMutableArray *items = [[NSMutableArray alloc] init];
        for(int i = 0 ; i < 10 ; i++){
            Possession *p = [Possession randomPossession];
            [items addObject:p];
        }
        for(Possession *item in items)
            NSLog(@"%@"  ,item);
        [items release];
        items = nil;
    }
    return 0;
}

 总的说对这个语言没啥特别的感觉,不过也不讨厌。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap