Objective-c语言_观察者模式
2015-12-18 19:06
423 查看
打开Xcode->选择IOS->Application->Single View Application->然后按next
2.如果你创建了工程那快捷键(com-shift-N)->选择IOS->Application->Single View Application->然后按next
在工程项下个找到ViewController.m文件
ViewController.m
#import "ViewController.h"
#import "Student.h"
#import "Book.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
设计模式--观察者模式
设计模式是用来解决某些特定问题
观察者模式
什么是观察者模式
当一个类需要发送消息给多个类的时候,就用观察者模式
什么时候是有观察者模式呢?
在工程中,一下类取观察A类,当‘A类’发生变化时,这些观察类就会收到消息,作出相应反应
观察者模式的作用?
一对多的发送消息;
如何在OC中实现观察者模式呢?
OC中观察者模式的设计基础“KVC/KVO”
KVC的由来在ios2.0之前,并没用属性来访问类中的实例变量,那时候开发人员是有‘键值编码’的方式
来访问类的实例变量,即‘KVC’的方式
什么是KVC (keh- Value Coding)键值编码
通过指定表示要访问的属性名字的字符串标识符,可以进行类的属性读取和设置
KVC提供了一种在运行时而非编译是动态访问对象属性与实例变量的方式。
*/
Student *student=[Student
new];
//赋值
[student setValue:@"Rick"
forKey:@"_name"];
[student setValue:@25
forKey:@"_age"];
[student setValue:@"GZ"
forKey:@"_address"];
NSLog(@"%@",[student
description]);
//访问
NSString *studentName=[student
valueForKey:@"_name"];//去掉下划线也能运行的
NSInteger studentAge=[student
valueForKey:@"_age"];
NSString *studentAddress=[student
valueForKey:@"_address"];
NSLog(@"studentName=%@ studentAge=%@ studentAddress=%@",studentName,studentAge,studentAddress);
//动态访问某些属性时,是有一些可以在运行时而不是编译时改变的值
的例子
student.p1=1;
student.p2=2;
student.p3=3;
int num=[student
getValueProoertyName:@"p1"];
NSLog(@"num=%d",num);
int num1=[student
getValueProoertyNewName:@"p2"];
NSLog(@"num1=%d",num1);
//键路径编码
Book *book=[Book
new];
//Book是student的成员变量
[student setValue:book
forKey:@"_book"];//forKeyPath可以不用forKey
//book对象赋给student的成员变量
[student setValue:@"Harry Potter"
forKeyPath:@"_book._bookName"];//访问student里面访问Book的成员变量
NSLog(@"bookName=%@",[book
description]);
NSString *bookName=[student
valueForKeyPath:@"_book._bookName"];
NSLog(@"bookName=%@",bookName);
/*
在student
@synthesize hobby=ttt;
封装变量
这样做就是会暴露实例的成员变量
*/
// [student setValue:@"play the guiter" forKey:@"_hobby"];
// NSLog(@"%@",[student valueForKey:@"_hoddy"]);//字典差不多意思是用key的名访问value
/*
在student
@synthesize hobby=ttt;
封装变量
这样做的原因之一就是不暴露实例的成员变量
*/
[student setValue:@"play the guiter"
forKey:@"ttt"];
NSLog(@"%@",[student
valueForKey:@"ttt"]);//字典差不多意思是用key的名访问value
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
=============================================================================
Student.h
#import <Foundation/Foundation.h>
#import "Book.h"
@interface Student :
NSObject
{
NSString *_name;
NSInteger _age;
Book *_book;
}
@property(nonatomic,strong)NSString *address;
@property(nonatomic)int p1;
@property(nonatomic)int p2;
@property(nonatomic)int p3;
-(int)getValueProoertyName:(NSString *)pName;
-(int)getValueProoertyNewName:(NSString *)pName;
@property(nonatomic,strong)NSString *hobby;
@end
====================================================================
Student.m
#import "Student.h"
@implementation Student
@synthesize hobby=ttt;//把hobby修改为ttt
-(NSString *)description
{
return [NSString
stringWithFormat:@"my name is %@ and age is %ld ,address =%@",_name,_age,_address];
}
-(int)getValueProoertyName:(NSString *)pName
{
if ([pName isEqualToString:@"p1"])
{
return self.p1;
}
else if ([pName
isEqualToString:@"p2"])
{
return self.p2;
}
else if([pName
isEqualToString:@"p3"])
{
return self.p3;
}
return 0;
}
-(int)getValueProoertyNewName:(NSString *)pName
{
NSNumber *pNumber=[self
valueForKey:pName];
return [pNumber
intValue];
}
@end
=================================================================
Book.h
#import <Foundation/Foundation.h>
@interface Book : NSObject
{
NSString *_bookName;
}
@end
==================================================================
Book.m
#import "Book.h"
@implementation Book
-(NSString *)description//他是在API他是类方法但现在用实例方法说明他是属性
{
return [NSString
stringWithFormat:@"bookName is %@",_bookName];
}
@end
2.如果你创建了工程那快捷键(com-shift-N)->选择IOS->Application->Single View Application->然后按next
在工程项下个找到ViewController.m文件
ViewController.m
#import "ViewController.h"
#import "Student.h"
#import "Book.h"
@interface
ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
设计模式--观察者模式
设计模式是用来解决某些特定问题
观察者模式
什么是观察者模式
当一个类需要发送消息给多个类的时候,就用观察者模式
什么时候是有观察者模式呢?
在工程中,一下类取观察A类,当‘A类’发生变化时,这些观察类就会收到消息,作出相应反应
观察者模式的作用?
一对多的发送消息;
如何在OC中实现观察者模式呢?
OC中观察者模式的设计基础“KVC/KVO”
KVC的由来在ios2.0之前,并没用属性来访问类中的实例变量,那时候开发人员是有‘键值编码’的方式
来访问类的实例变量,即‘KVC’的方式
什么是KVC (keh- Value Coding)键值编码
通过指定表示要访问的属性名字的字符串标识符,可以进行类的属性读取和设置
KVC提供了一种在运行时而非编译是动态访问对象属性与实例变量的方式。
*/
Student *student=[Student
new];
//赋值
[student setValue:@"Rick"
forKey:@"_name"];
[student setValue:@25
forKey:@"_age"];
[student setValue:@"GZ"
forKey:@"_address"];
NSLog(@"%@",[student
description]);
//访问
NSString *studentName=[student
valueForKey:@"_name"];//去掉下划线也能运行的
NSInteger studentAge=[student
valueForKey:@"_age"];
NSString *studentAddress=[student
valueForKey:@"_address"];
NSLog(@"studentName=%@ studentAge=%@ studentAddress=%@",studentName,studentAge,studentAddress);
//动态访问某些属性时,是有一些可以在运行时而不是编译时改变的值
的例子
student.p1=1;
student.p2=2;
student.p3=3;
int num=[student
getValueProoertyName:@"p1"];
NSLog(@"num=%d",num);
int num1=[student
getValueProoertyNewName:@"p2"];
NSLog(@"num1=%d",num1);
//键路径编码
Book *book=[Book
new];
//Book是student的成员变量
[student setValue:book
forKey:@"_book"];//forKeyPath可以不用forKey
//book对象赋给student的成员变量
[student setValue:@"Harry Potter"
forKeyPath:@"_book._bookName"];//访问student里面访问Book的成员变量
NSLog(@"bookName=%@",[book
description]);
NSString *bookName=[student
valueForKeyPath:@"_book._bookName"];
NSLog(@"bookName=%@",bookName);
/*
在student
@synthesize hobby=ttt;
封装变量
这样做就是会暴露实例的成员变量
*/
// [student setValue:@"play the guiter" forKey:@"_hobby"];
// NSLog(@"%@",[student valueForKey:@"_hoddy"]);//字典差不多意思是用key的名访问value
/*
在student
@synthesize hobby=ttt;
封装变量
这样做的原因之一就是不暴露实例的成员变量
*/
[student setValue:@"play the guiter"
forKey:@"ttt"];
NSLog(@"%@",[student
valueForKey:@"ttt"]);//字典差不多意思是用key的名访问value
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
=============================================================================
Student.h
#import <Foundation/Foundation.h>
#import "Book.h"
@interface Student :
NSObject
{
NSString *_name;
NSInteger _age;
Book *_book;
}
@property(nonatomic,strong)NSString *address;
@property(nonatomic)int p1;
@property(nonatomic)int p2;
@property(nonatomic)int p3;
-(int)getValueProoertyName:(NSString *)pName;
-(int)getValueProoertyNewName:(NSString *)pName;
@property(nonatomic,strong)NSString *hobby;
@end
====================================================================
Student.m
#import "Student.h"
@implementation Student
@synthesize hobby=ttt;//把hobby修改为ttt
-(NSString *)description
{
return [NSString
stringWithFormat:@"my name is %@ and age is %ld ,address =%@",_name,_age,_address];
}
-(int)getValueProoertyName:(NSString *)pName
{
if ([pName isEqualToString:@"p1"])
{
return self.p1;
}
else if ([pName
isEqualToString:@"p2"])
{
return self.p2;
}
else if([pName
isEqualToString:@"p3"])
{
return self.p3;
}
return 0;
}
-(int)getValueProoertyNewName:(NSString *)pName
{
NSNumber *pNumber=[self
valueForKey:pName];
return [pNumber
intValue];
}
@end
=================================================================
Book.h
#import <Foundation/Foundation.h>
@interface Book : NSObject
{
NSString *_bookName;
}
@end
==================================================================
Book.m
#import "Book.h"
@implementation Book
-(NSString *)description//他是在API他是类方法但现在用实例方法说明他是属性
{
return [NSString
stringWithFormat:@"bookName is %@",_bookName];
}
@end
相关文章推荐
- 《Object-OC 里面调用JS 和 接入是遇到的问题的解决》
- objective-c之谓词
- SuperMap iObjects .NET 8C技术文档 ——三维管线
- Android JSON解析(JSONObject和JSONArray)
- QAndroidJniObject类 部分函数使用方式
- com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1问题出现的原因及解决办法
- 动画绘制坐标系
- Android JSON的封装(JSONObject,JSONStringer两种方法)
- json to object
- 点击Cell进行多控制器跳转的简便写法
- Objective-C语法之KVC使用
- objective-c纯代码搭建抽屉项目(SWRevealViewController)
- iOS Objective-C 中 bool 与 BOOL 的你不一定知道的事
- iOS开发之Objective-C打开一个网页
- OC(objective-c)_runtime运行时官方文档翻译 -- api 速查表
- 对MarshalByRefObject的讲解
- C# MarshalByRefObject 和Serializable的区别
- object-c 宏定义
- UVALive-3645 Objective: Berlin (最大流:时序模型)
- Objective-C中@property所有的属性详解