编程技巧 - 封装技巧 - 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
- 编程技巧 - 封装技巧 - 1
- 编程技巧 - 封装技巧 - 2
- 编程小技巧之封装
- 封装技巧
- Internet编程技巧1
- IE编程技巧1
- Internet编程技巧1
- VC编程技巧1 .
- 编程技巧 - 1
- 编程技巧 - elegant - 1
- 编程技巧 - NSString - 1
- 编程技巧 - 适配技巧 - 1
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- 编程技巧
- su -c命令与环境变量的困惑
- java发送邮件的测试
- 安装和使用ansible
- VoltDb 性能测试
- 通过注解实现记录日志的功能
- 编程技巧 - 封装技巧 - 1
- 严重: Exception sending context initialized event to listener instance of class org.springframework.we
- CityEngine CGA语法之双坡式屋顶函数 roofGable
- 第13周实践项目-Dijkstra算法
- 第十一周实践项目~验证算法(2)二叉树构造算法的验证
- 第十三周项目1--Prim算法的验证
- java基础—IO流
- 第十周 项目3-4
- 第十三周项目3 Dijkstra算法的验证