iOS开发中extension的用法(延展)
来源:互联网 发布:软件项目经理工作内容 编辑:程序博客网 时间:2024/06/05 08:32
extension可以认为是匿名的category,但是这个extension相对于category有有一个特殊功能:
在extension中可以定义可写的属性,公有可读、私有可写的属性(Publicly-Readable, Privately-Writeable Properties)一般这样实现!
举例说明如下:
1. 创建测试程序empty application
2. 我们自定义一个UIViewController,命名为RootViewController,它的.h文件为:
01
//
02
// RootViewController.h
03
// Test4
04
//
05
// Created by Vincent on 13-5-29.
06
// Copyright (c) 2013年 DevDiv Community. All rights reserved.
07
//
08
09
#import <UIKit/UIKit.h>
10
11
@
interface
RootViewController : UIViewController
12
@end
那么在其对应的.m中会自动生成以下代码:
01
//
02
// RootViewController.m
03
// Test4
04
//
05
// Created by Vincent on 13-5-29.
06
// Copyright (c) 2013年 DevDiv Community. All rights reserved.
07
//
08
09
#import "RootViewController.h"
10
11
@
interface
RootViewController ()
12
@end
13
14
@implementation RootViewController
15
16
17
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
18
{
19
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
20
if
(self) {
21
// Custom initialization
22
}
23
return
self;
24
}
25
26
- (
void
)viewDidLoad
27
{
28
[super viewDidLoad];
29
// Do any additional setup after loading the view.
30
self.title =
@"RootController"
;
31
self.navigationItem
32
}
33
34
- (
void
)didReceiveMemoryWarning
35
{
36
[super didReceiveMemoryWarning];
37
// Dispose of any resources that can be recreated.
38
}
39
40
@end
3. 第2步中我们能看到
1
@
interface
RootViewController ()
2
@end
这个就是extension了(也就是特殊类型的category)
如果我们在.h添加这样一个属性
@property (readonly) float value;
那么RootViewController对外就暴露一个readonly的属性,它是公开的,所以外部是不能够对它进行写操作的。
这时我们可以在extension加入以下代码:
@property (readwrite) float value;
那么这个属性在内部就是可读写的了,如果是只读只能在构造时期对它赋值,其他类方法中是不能对其赋值的。
有了这个特性支持,那么类的内部方法均可以对其进行赋值了。
0 0
- iOS开发中extension的用法(延展)
- 延展(Extension)定义用法
- ios-延展Extension
- Objective-c中Extension(延展)的用法,Objective-c中的私有
- 5.2 iOS开发视频教程—延展的基本概念和用法
- iOS 类别(Category)&延展(Extension)
- 类别(类目)与延展 iOS Category Extension
- iOS中Extension开发是什么?
- iOS中协议和延展的区别
- iOS开发中常用的分类方法---UIImage+extension
- ios开发——日常之在Xcode6 后如何创建类目和延展(category&extension&protocol)
- iOS开发-类目、延展、协议
- iOS categoryde用法以及extension的区别
- iOS软件开发 类目延展协议的使用
- swift 2.0 延展 Extension
- Objective-c - 延展:extension
- iOS中 category 和extension的区别
- iOS中 category 和extension的区别
- Keepalived+LVS-DR模式配置高可用负载均衡集群
- c++第8题
- hdu 1711 Number Sequence
- C#中如何将int型转化为string型
- Android事件触发EditText焦点和自动弹出键盘
- iOS开发中extension的用法(延展)
- Android通过PHP连接MySQL(读取)
- Unity游戏开发的数学与物理 3 ( 让物体延任意方向移动 )
- android 下拉刷新控件
- 新浪微博、微信朋友圈、qq空间分享---微信朋友圈
- HTTP Status 404 There is no Action mapped for namespace / and action name
- C#连接SQL server
- Redis命令参考简体中文版
- ele.me答案系列之——第5题