编程技巧 - 封装技巧 - 1

来源:互联网 发布:linux 清除cache 编辑:程序博客网 时间:2024/05/16 04:49

1.用私有扩展封装一层来调用,帅!

#import "JSMessageSoundEffect.h"@interface JSMessageSoundEffect ()+ (void)playSoundWithName:(NSString *)name type:(NSString *)type;@end@implementation JSMessageSoundEffect+ (void)playSoundWithName:(NSString *)name type:(NSString *)type <span style="white-space:pre"></span>//首先先实现这个私有扩展的方法{    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:type];        if([[NSFileManager defaultManager] fileExistsAtPath:path]) {        SystemSoundID sound;        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &sound);        AudioServicesPlaySystemSound(sound);    }    else {        NSLog(@"Error: audio file not found at path: %@", path);    }}+ (void)playMessageReceivedSound{    [JSMessageSoundEffect playSoundWithName:@"messageReceived" type:@"aiff"];}+ (void)playMessageSentSound{    [JSMessageSoundEffect playSoundWithName:@"messageSent" type:@"aiff"];}@end

2.利用tag:

//添加动作作为参数:    privacyLabel = [[PrivacyLabel alloc] initWithFrame:CGRectMake(8, 20+44+20, 320-16, 40)                                            andTarget:self action:@selector(buttonClicked:)];//如果有多个不同动作,只传一个参数,则用tag//例子:        const NSInteger tag = ((UIButton*)sender).tag;        UIViewController* c = NULL;        if (tag == ACC_TAG)        {            c = [[AccSettingsController alloc] init];        }        else if (tag == SET_TAG)        {            c = [[MyAccountController alloc] init];        }        else if(tag == PRI_TAG)        {            c = [[PrivacyViewController alloc]init];        }        if (c != NULL)        {            [self.navigationController pushViewController:c animated:YES];        }


3.数据:

//封装数据的写法:例子一:#import <Foundation/Foundation.h>@interface TaskModel : NSObject<NSCopying>@property(nonatomic, copy)NSString* fromAccName;@property(nonatomic, copy)NSString* fromCompany;@property(nonatomic, copy)NSString* fromAccID;@property(nonatomic, copy)NSString* fromJID;@property(nonatomic, copy)NSString* subject;@property(nonatomic, copy)NSString* reson;@property(nonatomic, copy)NSString* type;@property(nonatomic, strong)UIImage* img;@property(nonatomic)double time1970;@end#import “TaskModel.h"//将封装数据存入本地内存@implementation TaskModel- (id)copyWithZone:(NSZone *)zone{    TaskModel* copy = [TaskModel allocWithZone:zone];    copy.img = [[UIImage alloc] initWithCGImage:self.img.CGImage];    copy.time1970 = self.time1970;    copy.fromCompany = self.fromCompany;    copy.fromAccName = self.fromAccName;    copy.fromAccID = self.fromAccID;    copy.fromJID = self.fromJID;    copy.subject = self.subject;    copy.reson = self.reson;    copy.type = self.type;    return copy;}@end


例子二:#import <Foundation/Foundation.h>@interface MemorandumModel : NSObject<NSCopying>@property(nonatomic)uint32_t mid;@property(nonatomic)double time1970;@property(nonatomic)BOOL done;@property(nonatomic, copy)NSString* time;@property(nonatomic, copy)NSString* title;@property(nonatomic, copy)NSString* content;@property(nonatomic, copy)NSString* creator;@property(nonatomic, copy)NSString* currentJid;@property(nonatomic, copy)NSArray* people;@end#import "MemorandumModel.h"@implementation MemorandumModel- (id)copyWithZone:(NSZone *)zone{    MemorandumModel* copy = [MemorandumModel allocWithZone:zone];    copy.mid = self.mid; copy.time1970 = self.time1970;    copy.time = self.time; copy.title = self.title;    copy.currentJid = self.currentJid;    copy.content = self.content;    copy.creator = self.creator;    copy.people = self.people;    copy.done = self.done;    return copy;}- (id)init{    self = [super init];    if (self)    {        NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];        self.currentJid = [[ud stringForKey:kXMPPmyJID] lowercaseString];        NSDate* currentDateTime = [NSDate date];        self.time1970 = [currentDateTime timeIntervalSince1970];        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];        [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss.SSS"];        self.time = [dateFormatter stringFromDate:currentDateTime];        self.done = false;    }    return self;}

以后会做个数据库的专题,结合coding所用的技术。因为这个copyWithZone技术相当旧。


0 0
原创粉丝点击