综合例子
这是目前为止最为复杂的例子,将前面的内容联系到一起的,主要是演示一个“新老师”俱乐部会员管理的实例:
Membership.h:
//
// Membership.h
// test
//
// Created by Dawn on 13-5-27.
// Copyright (c) 2013年 zeng. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Membership : NSObject<NSCoding>{
NSString *name;
NSString *telephone;
}
@property (nonatomic, copy) NSString *name, *telephone;
// 构建这个方法来一次性设置两个属性,简化操作
-(void) setName: (NSString *) theName andTelephone: (NSString *) theTelephone;
// 属性中的排序比较方法
-(NSComparisonResult) compareNames: (id) element;
-(void) print;
@end
Membership.m:
//
// Membership.m
// test
//
// Created by Dawn on 13-5-27.
// Copyright (c) 2013年 zeng. All rights reserved.
//
#import "Membership.h"
@implementation Membership
@synthesize name, telephone;
// 构建这个方法来一次性设置两个属性,简化操作
-(void) setName: (NSString *) theName andTelephone: (NSString *) theTelephone{
[self setName: theName];
[self setTelephone:theTelephone];
}
// 属性中的排序比较方法
-(NSComparisonResult) compareNames: (id) element{
// 通过compare方法比较属性的大小,将结果返回
return [name compare:[element name]];
}
-(void) print{
NSLog(@"会员的姓名是:%@", name);
NSLog(@"会员的手机号是:%@", telephone);
}
-(void) encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:name forKey:@"Membershipname"];
[encoder encodeObject:telephone forKey:@"Membershiptelephone"];
}
-(id) initWithCoder:(NSCoder *)decoder{
name = [[decoder decodeObjectForKey:@"Membershipname"] retain];
telephone = [[decoder decodeObjectForKey:@"Membershiptelephone" ] retain];
return self;
}
-(void) dealloc{
[name release];
[telephone release];
[super dealloc];
}
@end
YourClub.h
//
// YourClub.h
// test
//
// Created by Dawn on 13-5-27.
// Copyright (c) 2013年 zeng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Membership.h"
@interface YourClub : NSObject<NSCoding>{
NSString *clubname;
NSMutableArray *club;
}
@property (nonatomic, copy) NSString *clubname;
@property (nonatomic, copy) NSMutableArray *club;
-(id) initWithName: (NSString *) name;
-(void) sort;
-(void) addMember: (Membership *) theMember;
-(void) removeMember: (Membership *) theMember;
-(NSUInteger) entries;
-(void) list;
-(Membership *) lookup: (NSString *) theName;
-(void) dealloc;
@end
YourClub.m
//
// YourClub.m
// test
//
// Created by Dawn on 13-5-27.
// Copyright (c) 2013年 zeng. All rights reserved.
//
#import "YourClub.h"
@implementation YourClub
@synthesize club, clubname;
-(id) initWithName: (NSString *) name{
self = [super init];
if (self) {
clubname = [[NSString alloc] initWithString: name];
club = [[NSMutableArray alloc] init];
}
return self;
}
-(void) sort{
// 俱乐部数组中的元素的排序方法,选择器只想compareNames方法
[club sortUsingSelector:@selector(compareNames:)];
}
-(void) addMember: (Membership *) theMember{
[club addObject:theMember];
}
-(void) removeMember: (Membership *) theMember{
[club removeObject:theMember];
}
// 拿到俱乐部的成员总数
-(NSUInteger) entries{
return [club count];
}
-(void) list{
NSLog(@"%@会员列表:", clubname);
for (Membership *theMember in club) {
NSLog(@"%-10s %-10s", [theMember.name UTF8String], [theMember.telephone UTF8String]);
}
}
// 寻找俱乐部中的某个成员
-(Membership *) lookup: (NSString *) theName{
for (Membership *nextMember in club) {
if ([[nextMember name] caseInsensitiveCompare:theName] == NSOrderedSame) {
return nextMember;
}
}
return nil;
}
-(void) dealloc{
[clubname release];
[club release];
[super dealloc];
}
-(void) encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:clubname forKey:@"YourClubName"];
[encoder encodeObject:club forKey:@"YourClub"];
}
-(id) initWithCoder:(NSCoder *)decoder{
clubname = [[decoder decodeObjectForKey:@"YourClubName"] retain];
club = [[decoder decodeObjectForKey:@"YourClub"] retain];
return self;
}
@end
main.m:
//
// main.m
// test
//
// Created by Zeng on 13-5-24.
// Copyright (c) 2013年 zeng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "YourClub.h"
#import "Membership.h"
int main(int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Membership *member1 = [[Membership alloc] init];
Membership *member2 = [[Membership alloc] init];
Membership *member3 = [[Membership alloc] init];
Membership *member4 = [[Membership alloc] init];
Membership *member5 = [[Membership alloc] init];
[member1 setName:@"Alex" andTelephone:@"112233"];
[member2 setName:@"Sam" andTelephone:@"223344"];
[member3 setName:@"Kimi" andTelephone:@"334455"];
[member4 setName:@"Lee" andTelephone:@"445566"];
YourClub *club1 = [YourClub alloc];
YourClub *club2 = [YourClub alloc];
club1 = [club1 initWithName:@"新老师"];
[club1 addMember:member1];
[club1 addMember:member2];
[club1 addMember:member3];
[club1 addMember:member4];
NSLog(@"新老师共拥有的会员数为:%lu", [club1 entries]);
printf("\n");
[club1 list];
printf("\n");
NSLog(@"寻找会员Sam");
member5 = [club1 lookup:@"Sam"];
if (member5 == nil) {
NSLog(@"没有此会员的信息");
}else{
NSLog(@"该用户是我们的会员");
[member5 print];
}
printf("\n");
NSLog(@"寻找会员leee(并没有此会员)");
member5 = [club1 lookup:@"leee"];
if (member5 == nil) {
NSLog(@"没有此会员的信息");
}else{
NSLog(@"该用户是我们的会员");
[member5 print];
}
printf("\n");
NSLog(@"我们对会员进行排序(根据姓名升序排列):");
[club1 sort];
[club1 list];
printf("\n");
NSLog(@"删除用户Alex:");
member5 = [club1 lookup:@"Alex"];
if (member5 == nil) {
NSLog(@"我们没有这个会员,无法删除");
}else{
[club1 removeMember:member5];
NSLog(@"删除用户成功");
NSLog(@"删除该用户后的会员列表为:");
[club1 list];
}
printf("\n");
NSLog(@"将club1写入到文件club1.arch");
if ([NSKeyedArchiver archiveRootObject:club1 toFile:@"club1.arch"] == NO) {
NSLog(@"写入文件失败");
}else{
NSLog(@"写入文件成功");
}
printf("\n");
NSLog(@"从文件中读取数据");
club2 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"club1.arch"];
if (club2 == nil) {
NSLog(@"读取文件失败");
}else{
NSLog(@"读取文件成功");
[club2 list];
}
[member1 release];
[member2 release];
[member3 release];
[member4 release];
[pool release];
return 0;
}
输出如下:
AppKit和UIKit
1、图形化用户界面和cocoa
提供图形化界面的那些框架被统称为cocoa。它包含两个框架:基础框架和应用工具(Appkit)框架。在Mac操作系统和iPhone/iPad上的邮件程序、Safari等都是Cocoa应用程序。整个系统从顶向下分成五层:
操作系统内核:
同硬件的通讯、管理内存、执行I/O等
核心服务:
文件管理、网络、线程管理等
应用服务:
支持打印和图像,包括opengl等
cocoa:
管理窗口、视图等
iPhone/iPad所运行的操作系统叫做ios。在cocoa这一层,叫做cocoatouch。cocoa和cocoa touch都是用相同的基础框架。但是,在cocoa touch上,另一个框架是UIKit,而不是Appkit。在cocoa touch上,增加了一些类来吃吃GPS、触摸等功能。
AppKit
AppKit框架提供了界面上的所有对象,比如按钮、菜单等。在界面创建器中,开发人员把界面对象和代码关联,从而,当对象上的某个时间发生时,相关的代码就被调用。在AppKit的类层次树上,会发现,很多界面对象类都集成字NSResponder类(该类继承自NSObject类),比如,NSApplication、NSWindow和NSView等。
每个应用程序都是一个NSApplication实例,这个实例把时间发送到应用下的窗口对象。开发人员一般是先NSApplication对象的委托(回调)方法,从而控制着整个应用,比如,调用哪个窗口。
UIKit
UIKit是ios上的AppKit的变种,用于为ios应用程序提供界面对象和控制器。与AppKit类似,UIKit框架有UIResponder,也采用时间(UIEvent类)机制。另外,ios上的应用程序都是一个UIApplication实例(各个对象的创建都可以在P268中预览)。
多线程(NSOperation和NSOperationQueue)
在cocoa中国,NSOperation类提供了多线程编程方法。
在oc上,一个具体的操作是一个继承NSOperation的类。在着各类中,至少需要重写一个-(void)main方法。线程(NSOperation)自动调用main方法,main方法就是线程要执行的具体操作
P270一个例子
遇到了点问题:
http://www.cocoachina.com/bbs/read.php?tid=142764