美文网首页
二、object-c基础

二、object-c基础

作者: yezide | 来源:发表于2019-06-21 23:12 被阅读0次

1、类调用

1_ClassCall.m

#import <Foundation/Foundation.h>
#import "1_ClassDef.h"

int main(int argc, char* args[])
{
    @autoreleasepool
    {
        KFPerson* person;
        person = [[KFPerson alloc] init];

        [person say:@"孙中山"];
        [person
            setName: @"古天乐"
            andAge: 41];
        NSString* myInfoStr = [person info];
        NSLog(@"info: %@", myInfoStr);

        [KFPerson foo];
    }
}

1_ClassDef.h

#import <Foundation/Foundation.h>

@interface KFPerson : NSObject
{
    // 定义变量
    NSString* _name;
    int _age;
}

// 定义setName:andAge方法
- (void) setName: (NSString*) name andAge: (int) age;

- (void) say: (NSString*) content;

- (NSString*) info;

+ (void) foo;
@end

1_ClassDef.m

#import <Foundation/Foundation.h>
#import "1_ClassDef.h"

@implementation KFPerson
{
    // 此变量只能在impl部份使用
    int _testAttr;
}

- (void) setName: (NSString*) n andAge: (int) a
{
    _name = n;
    _age = a;
}

- (void) say: (NSString*) c
{
    NSLog(@"hello: %@", c);
}

- (NSString*) info
{
    [self test];
    // return [NSString stringWithFormat: @"我是一个人,名字是%@, 年龄为%d" ,_name , _age];
    return _name;
}

- (void) test
{
    NSLog(@"我只能在impl部份被调用");
}

+ (void) foo
{
    NSLog(@"可以直接通过类名调用, 相当于JAVA的static方法");
}

@end

2、函数

2_Function.h

#import <Foundation/Foundation.h>

@interface KFUser1 : NSObject
{
    @public
    NSString* test;
    NSString* _test;
}
@property NSString* name;
@property NSString* pass;
@property NSDate* birth;
@end

2_Function.m

#import "2_Function.h"

@implementation KFUser1

- (void) setValue: (id) value forUndefinedKey: (id) key
{
    NSLog(@"setValue:forUndefinedKey key[%@] is not exists, value = %@", key, value);
}
@end

2_FunctionTest.m

#import <Foundation/Foundation.h>
#import "2_Function.h"

int main(int argc, char* args[])
{
    @autoreleasepool
    {
        KFUser1* user1;
        user1 = [[KFUser1 alloc] init];

        [user1 setName: @"jingle.ljl"];
        NSLog(@"name=%@", [user1 name]);

        [user1 setValue:@"testValue" forKey:@"test"];
        NSLog(@"test-value=%@", user1->test);
        NSLog(@"_test-value=%@", user1->_test);

        [user1 setValue:@"testValue" forKey:@"test1"];

        user1->test = @"testvalue1";
        NSLog(@"value=%@", user1->test);
    }
}

KFItem.h

#import <Foundation/Foundation.h>

@interface KFItem : NSObject
@property (nonatomic, copy) NSString* name;
@property (nonatomic, assign) int price;
@end

KFItem.m

#import "KFItem.h"

@implementation KFItem
@end

KFItemView.h

#import <Foundation/Foundation.h>

#import "KFItem.h"

@interface KFItemView : NSObject
@property(nonatomic, weak) KFItem* item;
- (void) showItemInfo;
@end

KFItemView.m

#import "KFItemView.h"

@implementation KFItemView
- (void) showItemInfo
{
    NSLog(@"item  物品名为%@, 价格为%d", self.item.name, self.item.price);
}

- (void) setItem: (KFItem*) item
{
    self->_item = item;
    // 为item添加监听器
    [self.item addObserver: self
        forKeyPath: @"name"
        options: NSKeyValueObservingOptionNew
        context: nil];

    [self.item addObserver: self
        forKeyPath: @"price"
        options: NSKeyValueObservingOptionNew
        context: nil];
}

// 重写此方法. 当数据模型改变时, 回调监听器的该方法
- (void) observeValueForKeyPath: (NSString*) keyPath
   ofObject: (id) object
   change: (NSDictionary*) change
   context: (void*) context
{
    NSLog(@"--observerValueForKeyPath被调用---");
    NSLog(@"keyPath=%@, object=%@, change=%@, context=%@", keyPath, object, [change objectForKey:@"new"], context);
}

- (void) dealloc
{
    [self.item removeObserver: self forKeyPath: @"name"];
    [self.item removeObserver: self forKeyPath: @"price"];
}
@end

KFItemViewTest.m

#import "KFItemView.h"

int main(int argc, char* args[])
{
    @autoreleasepool
    {
        KFItem* item = [[KFItem alloc] init];
        item.name = @"星吧克";
        item.price = 5.0;

        KFItemView* view = [[KFItemView alloc] init];
        view.item = item;
        [view showItemInfo];

        item.name = @"涨价啦";
        item.price = 6.0;
    }
}

相关文章

  • Object-C学习索引

    本系列需要有一定代码基础,总结要点,快速掌握 一、Object-C 对象、消息和类的定义二、Object-C 对象...

  • Effective Object-C 52:1-5

    一、熟悉Object-C Object-C : 基于C语言基础 + 面向对象特性。 1.了解Object-C 的起...

  • iOS ReactiveCocoa

    Object-C编 ReactiveCocoa基础用法

  • 二、object-c基础

    1、类调用 1_ClassCall.m 1_ClassDef.h 1_ClassDef.m 2、函数 2_Func...

  • realm-cocoa 学习

    PS:以下内容均为项目为Swift的基础,如果需要Object-c请查阅Realm的Object-c文档。 前些日...

  • object-c 基础二 【实现构造函数】

    案例说明: 【h文件】 1.无参数 - (id) init; 在object中,id表示任何一个继承NSObjec...

  • (GeekBand第一周)Object C编程语言

    Object-C简介 1980年代由Brad Cox和Tim Love发明。OC较C语言基础上做了面向对象的基础,...

  • object-c 基础一 【初始object-c】

    一、object-c的一些基础认识 1.object-c中所有对象必须继承基类【NSObject】 2、所有关键字...

  • IOS object-c基础

    ## 第一讲:OC简介及基本语法 Objective-C简称OC是在C语言的基础上,增加了一层最小的面向对象语法,...

  • Object-c 基础详解

    Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值...

网友评论

      本文标题:二、object-c基础

      本文链接:https://www.haomeiwen.com/subject/ugupqctx.html