一.NSObjec和反射
反射:根据字符串来实例化一个对象
看先代码:
-
//
-
// Person.h
-
// NSobject
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
-
@interface Person : NSObject
-
-
-(void)test;
-
-
@end
-
//
-
// Person.m
-
// NSobject
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Person.h"
-
-
@implementation Person
-
-
-(void)test{
-
-
-
NSLog(@"test");
-
-
}
-
-
-
@end
-
//
-
// Student.h
-
// NSobject
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Person.h"
-
-
@interface Student : Person
-
-
-(void)fun;
-
-(void)fun1:(NSString *)a;
-
-
@end
-
//
-
// Student.m
-
// NSobject
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Student.h"
-
-
@implementation Student
-
-
-(void)fun{
-
-
NSLog(@"fun");
-
-
}
-
-
-(void)fun1:(NSString*)a{
-
-
-
NSLog(@"fun1111---%@",a);
-
-
}
-
-
@end
-
//
-
// main.m
-
// NSobject
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
#import "Person.h"
-
-
void fun(){
-
//反射
-
//类名的反射
-
NSString *s= @"Student";
-
-
Class class = NSClassFromString(s);
-
-
Person *p = [[class alloc]init ];
-
-
NSLog(@"%@",p );
-
-
[p release];
-
-
NSString *s1 = NSStringFromClass([Person class]) ;
-
//方法的反射
-
NSString *s2 = @"test";
-
-
SEL selector = NSSelectorFromString(s2);
-
[p performSelector:selector];
-
-
NSString* s3 = NSStringFromSelector(selector);
-
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
Student *s=[[[Student alloc]init]autorelease];
-
-
//判断对象是不是他的子类或者是类
-
//[Student class] 返回类名
-
if ([s isKindOfClass:[Person class]] ){
-
-
NSLog(@"OK");
-
-
}
-
-
//判断对象是不是他的类
-
if ([s isMemberOfClass:[Person class]]) {
-
NSLog(@"OK11");
-
}
-
-
-
//直接调用
-
//[s fun];
-
//间接调用
-
[s performSelector:@selector(fun)];
-
[s performSelector:@selector(fun1:) withObject:@"adf"];
-
//延迟调用
-
[s performSelector:@selector(fun1) withObject:@"123" afterDelay:2];
-
-
fun();
-
-
}
-
return 0;
-
}
二.COPY
要实现copy,需要先实现NSCoppying协议,创建的是不可变副本
要实现mutableCopy,需要实现NSMutableArray协议,创建的时可变的副本
深拷贝,对象的拷贝,mutableCopy
浅拷贝,指针的拷贝,copy
看下面代码:
-
//
-
// Student.h
-
// Copy
-
//
-
// Created by 金海洋 on 15-4-23.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Cocoa/Cocoa.h>
-
-
//实现NSCopying协议
-
@interface Student : NSObject<NSCopying>
-
-
//copy代表 set方法里面release就对象,copy新对象
-
//修改外面的变量,不会影响到内部变量
-
@property (nonatomic,copy) NSString *name;
-
-
+(id)studentWithName:(NSString*)name;
-
-
@end
-
//
-
// Student.m
-
// Copy
-
//
-
// Created by 金海洋 on 15-4-23.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Student.h"
-
-
@implementation Student
-
-
-
-(void)setName:(NSString *)name{
-
-
if(_name != name){
-
-
[_name release];
-
_name = [name copy];
-
-
-
}
-
-
-
}
-
-
-
-(void)dealloc{
-
-
[_name release];
-
[super dealloc];
-
-
-
}
-
-
+(id)studentWithName:(NSString*)name{
-
//最好写成 self class
-
Student *s =[[[[self class] alloc]init]autorelease];
-
s.name = name;
-
return s;
-
-
}
-
-
//实现copyWithZone方法
-
- (id)copyWithZone:(NSZone *)zone{
-
-
Student *copy = [[[self class] allocWithZone:zone]init];
-
//拷贝名字给副本对象
-
copy.name = self.name;
-
-
return copy;
-
//不需要释放,因为由外部来释放
-
}
-
-
- (NSString *)description
-
{
-
return [NSString stringWithFormat:@"name--%@", _name];
-
}
-
-
@end
-
//
-
// Stu.h
-
// Copy
-
//
-
// Created by 金海洋 on 15-4-23.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
@interface Stu : Student
-
-
-
@property (nonatomic, assign) int age;
-
@property (nonatomic, copy) NSString *name;
-
-
+(id)stuWithAge:(int)age name:(NSString*)name;
-
-
@end
-
//
-
// Stu.m
-
// Copy
-
//
-
// Created by 金海洋 on 15-4-23.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import "Stu.h"
-
-
@implementation Stu
-
-
-
+(id)stuWithAge:(int)age name:(NSString *)name{
-
-
Stu *s = [Stu studentWithName:@"sfg"];
-
-
s.age = age;
-
-
return s;
-
-
}
-
-
- (id)copyWithZone:(NSZone *)zone{
-
-
//一定要调用父类的方法
-
Stu *s =[super copyWithZone:zone];
-
-
s.age = self.age;
-
-
return s;
-
}
-
-
//子类直接访问不了父类的变量,必须通过get方法访问
-
- (NSString *)description
-
{
-
return [NSString stringWithFormat:@"name--%@,age-%d", self.name,_age];
-
}
-
-
@end
-
//
-
// main.m
-
// Copy
-
//
-
// Created by 金海洋 on 15-4-17.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
#import "Stu.h"
-
-
void fun(){
-
//字符串拷贝
-
-
NSString *s = [[NSString alloc]initWithString:@"afa"];
-
//新的对象计数器+1
-
NSMutableString * s1 = [s mutableCopy];
-
-
NSLog(@"s---%zd",[s retainCount]);
-
NSLog(@"s1---%zd",[s1 retainCount]);
-
-
//产生的对象不一样
-
if (s != s1) {
-
NSLog(@"不一样");
-
}
-
-
[s1 appendString:@"asdfasdf"];
-
-
//不会改变原来的值
-
NSLog(@"s1---%@",s1);
-
NSLog(@"s---%@",s);
-
-
[s release];
-
[s1 release];
-
-
}
-
-
void fun1(){
-
-
-
NSString *s = [[NSString alloc]initWithString:@"afa"];
-
-
NSLog(@"s---%zd",[s retainCount]);
-
-
//为了性能,因为copy产生的是不可变对象,所以产生的是s本身,直接返回原对象本身,源对象计数区加1
-
//相当于retain
-
//只有不可变对象调用copy的时候,是浅拷贝
-
NSString *s1 = [s copy];
-
-
NSLog(@"s---%zd",[s retainCount]);
-
-
if (s == s1) {
-
NSLog(@"一样");
-
}
-
-
[s release];
-
[s release];
-
-
}
-
-
void fun2(){
-
-
-
NSMutableString *s =[NSMutableString stringWithFormat:@"123"];
-
-
//这个时候是深拷贝,因为一个是可变的
-
//产生了个新对象,计数器+1
-
NSMutableString *s1 = [s copy];
-
-
if (s != s1) {
-
NSLog(@"不一样");
-
}
-
-
[s release];
-
[s1 release];
-
-
}
-
-
-
void fun3(){
-
-
Student *s = [[[Student alloc]init]autorelease];
-
NSMutableString *s1 = [NSMutableString stringWithFormat:@"123"];
-
s.name = s1;
-
//因为set方法里用到的是copy,所以不会改变内部变量
-
[s1 appendString:@"345"];
-
//建议:String用copy,其他变量用retain
-
NSLog(@"%@",s.name);
-
-
}
-
-
-
void fun4(){
-
-
Student *s =[Student studentWithName:@"123"];
-
-
Student *s1 =[s copy];
-
-
s1.name = @"444";
-
-
NSLog(@"%@",s1);
-
NSLog(@"%@",s);
-
-
-
}
-
-
void fun5(){
-
-
-
Stu *s = [Stu stuWithAge:445 name:@"sdf"];
-
-
Stu *s1 = [s copy];
-
-
s1.name = @"ddd";
-
s1.age = 32222;
-
-
NSLog(@"%@",s1);
-
-
NSLog(@"%@",s);
-
-
}
-
-
//copy语法的目的,改变副本的时候,不会影响到源对象
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
-
//fun();
-
//fun1();
-
//fun2();
-
//fun3();
-
//fun4();
-
fun5();
-
-
}
-
return 0;
-
}
阅读(739) | 评论(0) | 转发(0) |