概述
#import <Foundation/Foundation.h>
@interface NSString (countNum)
-(int)count;
@end
#import "NSString+countNum.h"
@implementation NSString (countNum)
-(int)count{
NSUInteger NSLength = [self length];
int sum = 0;//定义变量
//循环判断
for (int i = 0; i < NSLength;i++) {
unichar ch = [self characterAtIndex:i];
if (ch >= '0' && ch <= '9') {
sum++;
}
}
//返回变量;
return sum;
}
@end
//
非正式协议
//
//
Created by MAC on 15/11/25.
//
Copyright © 2015年 zhudong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NSString+countNum.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"123 aa aaa";
int countNum = [str count];
NSLog(@"countNum = %d",countNum);
}
return 0;
}
<1> autorelease注意及使用方法
(1)并不是将对象代码放到自动释放池autorelease中,对象就加入了自动释放池;
(2)对象调用了autorelease方法,但是并不在autorelease中,也不会被加入到自动释放池;
(3)如果纯在自动释放池的嵌套问题,对象被放在栈顶的释放池中;
(4)自动释放池中,不宜存放占用内存过大的对象;不宜把循环放到同一个autorelease中;
<2> autorelease应用场景
(1)[[[self alloc] init] autorelease]//实现创建类方法,创建实例对象并自动释放内存空间;
(2)id和instancetype的区别:使用instancetype类型时,编译器会监测指针变量的类型和返回的
类型是否一致;
<3> ARC概念和原理
(1)指针分类:强指针(__strong)和弱指针(__week);
(2)ARC判断原则:没有强指针指向对象时,对象内存空间就会被释放;
(3)ARC中可以重写dealloc方法,但是不能用[super dealloc];
<4> ARC下单对象内存管理
(1)ARC是OC编译下的特性,而非运行时的特性,也不是垃圾回收机制;
(2)弱指针被释放后,被赋值为nil;
<5> ARC下多对象内存管理
(1)一般:@property(nonatomic strong);
(2)@property的修饰参数strong、week类似MRC下的retain,assign;当ARC下出现@property的循
环引用的问题时,可以用一端使用assign,另一端使用retain的方法来解决;
<6> MRC和ARC的转换
(1)ARC兼容非ARC的类:_fno_objc_ARC;
(2)MRC转换成ARC:edit/refactor/convert to ARC;
<7> 分类的概念和流程
(1)分类(category)是OC的特有语法,用来给既定类扩展方法;
(2)分类不可扩展对象变量;不可使用@property;分类中可以使用原类的变量;
(3)当分类中出现和原类中同名的方法时,首先调用分类的方法;当分类中出现同名的方法时,调用
分类中最后编译分类的方法;
<8> 非正式协议
(1)定义:NSObject或者其子类Foundation下的分类。
<9> 延展
(1)延展中既可以扩展类的方法,也可以增加定义类的变量;
<10> block的概念和使用
(1)block同时兼容C、OC、C++;
(2)格式:int (^myblock)(int,int) = (int a,int b){ 代码;return };
下面有几个实例练习:
1
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic,assign) int age;
+(instancetype)student:(int) age;//定义类方法,得到默认对象,并能加入到自动释放池
-(instancetype)initWithAge:(int) age;//定义自定义初始化方法
@end
#import "Student.h"
@implementation Student
//实现自定义方法,该方法调用了自定义构造方法,并且加入到了自动释放池
+(instancetype)student:(int) age{
return [[[self alloc] initWithAge:age] autorelease];
}
//实现自定义构造方法
-(instancetype)initWithAge:(int) age{
if (self = [super init]) {
_age = age;
}
return self;
}
//重写对象dealloc方法,能够清楚检查对象释放
-(void)dealloc{
NSLog(@"student dealloc");
[super dealloc];
}
@end
//
autorelease和自定义构造方法
//
//
Created by MAC on 15/11/23.
//
Copyright © 2015年 zhudong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu
= [Student student:18];
}
return 0;
2
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
#import "Person.h"
@implementation Person
@end
#import "Person.h"
@interface Person (playGame)
-(void)playLOL;
-(void)playQQ;
@end
#import "Person+playGame.h"
@implementation Person (playGame)
-(void)playLOL{
NSLog(@"PLAYLOL");
}
-(void)playQQ{
NSLog(@"PLAYQQ");
}
@end
//
分类的声明和实现
//
//
Created by MAC on 15/11/24.
//
Copyright © 2015年 zhudong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Person+playGame.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *p = [Person new];
[p playLOL];
[p playQQ];
}
return 0;
}
3
#import <Foundation/Foundation.h>
@interface NSString (countNum)
-(int)count;
@end
#import <Foundation/Foundation.h>
#import "NSString+countNum.h"
@implementation NSString (countNum)
-(int)count{
NSUInteger NSLength = [self length];
int sum = 0;//定义变量
//循环判断
for (int i = 0; i < NSLength;i++) {
unichar ch = [self characterAtIndex:i];
if (ch >= '0' && ch <= '9') {
sum++;
}
}
//返回变量;
return sum;
}
@end
//
非正式协议
//
//
Created by MAC on 15/11/25.
//
Copyright © 2015年 zhudong. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NSString+countNum.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"123 aa aaa";
int countNum = [str count];
NSLog(@"countNum = %d",countNum);
}
return 0;
}
4
//
Block的使用
//
//
Created by MAC on 15/11/25.
//
Copyright © 2015年 zhudong. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//block实现两个整数的和与乘积
int (^myBlock)(int,int)= ^(int a,int b){
return
a + b;
};
int result = myBlock(3, 2);
myBlock = ^(int x ,int y){
return x*y;
};
result = myBlock(5,4);
NSLog(@"result = %d",result);
}
return 0;
}
最后
以上就是生动铅笔为你收集整理的OC基础-ARC和MRC的理解及应用的全部内容,希望文章能够帮你解决OC基础-ARC和MRC的理解及应用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复