锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 涓 緋葷粺鍐呯疆瀵硅薄璇誨啓鑷蟲枃浠?span> Program 19.1 #import <Foundation/NSObject.h> #import <Foundation/NSString.h> #import <Foundation/NSDictionary.h> #import <Foundation/NSAutoreleasePool.h> int main (int argc, char *argv[])
int main (int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSDictionary *glossary = [NSDictionary dictionaryWithObjectsAndKeys: @”A class defined so other classes can inherit from it.”, @”abstract class”, @”To implement all the methods defined in a protocol”, @”adopt”, @”Storing an object for later use. “, @”archiving”, nil ]; if ([glossary writeToFile: @”glossary” atomically: YES] == NO) NSLog (@”Save to file failed!”); [pool drain]; return 0; } - To read an XML property list from a file into your program, you use the dictionaryWithContentsOfFile: or arrayWithContentsOfFile: methods.To read back data, use the dataWithContentsOfFile: method; to read back string objects, use the stringWithContentsOfFile:method. Program 19.2 reads back the glossary written in Program 19.1 and then displays its contents.
#import <Foundation/NSObject.h> #import <Foundation/NSString.h> #import <Foundation/NSDictionary.h> #import <Foundation/NSEnumerator.h> #import <Foundation/NSAutoreleasePool.h> int main (int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSDictionary *glossary; glossary = [NSDictionary dictionaryWithContentsOfFile: @”glossary”]; for ( NSString *key in glossary ) NSLog (@”%@: %@”, key, [glossary objectForKey: key]); [pool drain]; return 0; } 浜?鑷畾涔夊璞¤鍐欒嚦鏂囦歡 - A more flexible approach enables you to save any type of objects to a file, not just strings, arrays, and dictionaries.This is done by creating a keyed archive using the NSKeyedArchiver class.
]]>
]]>
]]>
]]>
http://sourceforge.net/projects/packetpeeper/
]]>
Javascript涔熸槸涓鏍風殑閬撶悊錛屼絾鏄娉ㄦ剰錛?js鏂囦歡榛樿浼氳褰撲綔浠g爜錛屾墍浠ユ妸js鏂囦歡鍔犲叆宸ョ▼鍚庯紝闇瑕佸埌XCode宸︽爮鐨勮祫婧愭爲錛屾壘鍒癟argerts->(浣犵殑紼嬪簭鍚嶅瓧)->Compile Sources閭i噷錛屾壘鍒版柊鍔犲叆鐨刯s鏂囦歡錛屾妸瀹冧滑鎷栧埌Targerts->(浣犵殑紼嬪簭鍚嶅瓧)->Copy Bundle Resources閲岄潰鍘伙紝灝卞彲浠ヤ簡銆?br style="LINE-HEIGHT: normal; WORD-WRAP: break-word">
鍔犲叆鏂囦歡鍒伴」鐩殑鏃跺欐湁涓や釜閫夐」錛屼竴涓彨鍋?#8220;Recursively create groups for any added folders”涓涓彨鍋?#8220;Create Folder References for any added folders”銆傛垜浠竴鑸敤鍓嶈咃紝榪欐牱鎵鏈夊姞榪涙潵鐨勬枃浠墮兘鍦ㄨ祫婧愮洰褰曠殑鏍圭洰褰曘傚鏋滀綘闇瑕佷綘鐨凧S鎴栬匔SS鏈夌洰褰曠粨鏋勶紝涓嶈窡HTML鍦ㄤ竴璧鳳紝鑰屾槸鏈夌浉瀵硅礬寰勫叧緋葷殑璇濓紝浣犲彲浠ョ敤絎簩涓夐」錛屽姞鍏ヤ竴涓洰褰曡繘鏉ワ紝鐩綍閲岄潰鐨勬枃浠惰繘鍏ヨ祫婧愮殑鏃跺欎細淇濇寔鐩稿璺緞鍏崇郴銆?/span>
]]>
]]>
- Programming.in.Objective-C.2.0.2nd(Addison.Wesley.2009)
褰掓。錛孨SCopying錛孨SCoding
]]>
- If your objects are of type NSString,NSDictionary,NSArray,NSDate,NSData, or NSNumber, you can use the writeToFile:atomically: method implemented in these classes to write your data to a file. In the case of writing out a dictionary or an array, this method writes the data to the file in the format of an XML property list. Program 19.1 shows how the dictionary you created as a simple glossary in Chapter 15,“Numbers, Strings, and Collections,” can be written to a file as a property list.
This implies that you can’t directly archive your AddressBook using this technique because the Objective-C system doesn’t know how to archive an AddressBook object. If you tried to archive it by inserting a line such as NSKeyedArchiver archiveRootObject: myAddressBook toFile: @”addrbook.arch”]; into your program, you’d get the following message displayed if you ran the program:
*** -[AddressBook encodeWithCoder:]: selector not recognized
*** Uncaught exception: <NSInvalidArgumentException>
*** -[AddressBook encodeWithCoder:]: selector not recognized
archiveTest: received signal: Trace/BPT trap
To archive objects other than those listed, you must tell the system how to archive, or encode, your objects, and also how to unarchive, or decode, them.This is done by adding encodeWithCoder: and initWithCoder: methods to your class definitions, according to the <NSCoding> protocol. For our address book example, you’d have to add these methods to both the AddressBook and AddressCard classes.
The encodeWithCoder: method is invoked each time the archiver wants to encode an object from the specified class, and the method tells it how to do so. In a similar manner, the initWithCoder: method is invoked each time an object from the specified class is to be decoded.
Program 19.5 Addresscard.h Interface File
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyedArchiver.h>
@interface AddressCard: NSObject <NSCoding, NSCopying>
{
NSString *name;
NSString *email;
}
@property (copy, nonatomic) NSString *name, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(NSComparisonResult) compareNames: (id) element;
-(void) print;
// Additional methods for NSCopying protocol
-(AddressCard *) copyWithZone: (NSZone *) zone;
-(void) retainName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) encodeWithCoder: (NSCoder *) encoder
{
[encoder encodeObject: name forKey: @”AddressCardName”];
[encoder encodeObject: email forKey: @”AddressCardEmail”];
}
-(id) initWithCoder: (NSCoder *) decoder
{
name = [[decoder decodeObjectforKey: @”AddressCardName”] retain];
email = [[decoder decodeObjectforKey: @”AddressCardEmail”] retain];
return self;
}
#import “AddressBook.h”
#import <Foundation/NSAu orelea ePool.h>
int main (int argc, char *argv[])
{
NSString *aName = @”Julia Kochan”;
NSString *aEmail = @”jewls337@axlc.com”;
NSString *bName = @”Tony Iannino”;
NSString *bEmail = @”tony.iannino@techfitness.com”;
NSString *cName = @”Stephen Kochan”;
NSString *cEmail = @”steve@steve_kochan.com”;
NSString *dName = @”Jamie Baker”;
NSString *dEmail = @”jbaker@hitmail.com”;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];
AddressBook *myBook = [AddressBook alloc];
// First set up four address cards
[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];
myBook = [myBook initWithName: @”Steve’s Address Book”];
// Add some cards to the address book
[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
[myBook sort];
if ([NSKeyedArchiver archiveRootObject: myBook toFile: @”addrbook.arch”] == NO)
NSLog (@”archiving failed”);
[card1 release];
[card2 release];
[card3 release];
[card4 release];
[myBook release];
[pool drain];
return 0;
}
Program 19.7 shows how you can read the archive into memory to set up the address
book from a file.
Program 19.7
#import “AddressBook.h”
#import <Foundation/NSAutoreleasePool.h>
int main (int argc, char *argv[])
{
AddressBook *myBook;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
myBook = [NSKeyedUnarchiver unarchiveObjectWithFile: @”addrbook.arch”];
[myBook list];
[pool drain];
return 0;
}
涓?浣跨敤NSData璇誨啓鏂囦歡
- You might not want to write your object directly to a file using the archiveRootObject:ToFile: method, as was done in the previous program examples.For example, perhaps you want to collect some or all of your objects and store them in a single archive file.You can do this in Objective-C using the general data stream object class called NSData.
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *myFoo1 = [[Foo alloc] init];
Foo *myFoo2;
NSMutableData *dataArea;
NSKeyedArchiver *archiver;
AddressBook *myBook;
// Insert code from Program 19.7 to create an Address Book
// in myBook containing four address cards
[myFoo1 setStrVal: @”This is the string”];
[myFoo1 setIntVal: 12345];
[myFoo1 setFloatVal: 98.6];
// Set up a data area and connect it to an NSKeyedArchiver object
dataArea = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData: dataArea];
// Now we can begin to archive objects
[archiver encodeObject: myBook forKey: @”myaddrbook”];
[archiver encodeObject: myFoo1 forKey: @”myfoo1”];
[archiver finishEncoding];
// Write the archived data are to a file
if ( [dataArea writeToFile: @”myArchive” atomically: YES] == NO)
NSLog (@”Archiving failed!”);
[archiver release];
[myFoo1 release];
[pool drain];
return 0;
}
- Restoring the data from your archive file is simple:You just do things in reverse. First, you need to allocate a data area like before.Next, you need to read your archive file into the data area; then you have to create an SKeyedUnarchiver object and tell it to decode data from the specified area.You must invoke decode methods to extract and decode your archived objects.When you’re finished, you send a finishDecoding message to the NSKeyedUnarchiver object.
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSCoder.h>
#import <Foundation/NSData.h>
#import “AddressBook.h”
#import “Foo.h”
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSData *dataArea;
NSKeyedUnarchiver *unarchiver;
Foo *myFoo1;
AddressBook *myBook;
// Read in the archive and connect an
// NSKeyedUnarchiver object to it
dataArea = [NSData dataWithContentsOfFile: @”myArchive”];
if (! dataArea) {
NSLog (@“Can’t read back archive file!”);
Return (1);
}
unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData: dataArea];
// Decode the objects we previously stored in the archive
myBook = [unarchiver decodeObjectForKey: @”myaddrbook”];
myFoo1 = [unarchiver decodeObjectForKey: @”myfoo1”];
[unarchiver finishDecoding];
[unarchiver release];
// Verify that the restore was successful
[myBook list];
NSLog (“%@\n%i\n%g”, [myFoo1 strVal],
[myFoo1 intVal], [myFoo1 floatVal]);
[pool release];
return 0;
}
鍙傝冿細Programming.in.Objective-C.2.0.2nd(Addison.Wesley.2009)
瀹?
]]>
Ios 4.0 澶氫換鍔′笉鏄紶緇熸剰涔変笂鐨勫浠誨姟銆傚彧鏄妸紼嬪簭鐨勭姸鎬佷繚瀛樿搗鏉ワ紝紼嬪簭鎸傝搗銆傚洜涓?/span>Apple榪樻病鍑嗗濂藉浠誨姟鍚屾椂榪愯錛?/span>
涓昏鏄洜涓?/span>battery鍜?/span>memory榪欎袱涓棶棰樿繕娌℃湁瑙e喅銆?/span>
鐜板湪IOS 4澶氫換鍔℃敮鎸佺殑綾誨瀷錛堝畼緗戯級錛?/span>
§ Background
audio
§ Voice
over IP
§ Background
location
§ Push
notifications
§ Local
notifications
§ Task
finishing - If your app is in mid-task when your customer leaves it,
the app can now keep running to finish the task.
§ Fast
app switching - All developers should take
advantage of fast app switching, which allows users to leave your app and come
right back to where they were when they left - no more having to reload the
app.
鎴戜嬌鐢ㄧ殑鏄?/font>Task
finishing錛?/span> 鏃㈠綋鐢ㄦ埛鎸傝搗紼嬪簭鏃訛紝濡傛灉榪樻湁task娌″畬鎴愶紝鍙互鎶婃敼task瀹屾垚銆?/span>
浣嗚繖涓槸鏈夐檺鍒剁殑錛屾椂闂寸殑闄愬埗錛屽氨鏄浣犵殑鍚庡彴紼嬪簭涓嶈兘鎵ц瓚呰繃鏌愪釜鏃墮棿銆?/span>
鎴戝垰鎵嶆墦log鐪嬩簡錛岀郴緇熻繑鍥?/span>500s錛屾棦鏄?/span>8鍒嗛挓錛?/span>8鍒嗛挓濡傛灉榪樻病鎵ц瀹岋紝灝變細鑷姩鎶婃垜浠▼搴忕粨鏉熴?/span>
浠g爜濡備笅
#pragma mark -
#pragma mark Background Task Handle
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Request permission to run in the background. Provide an
// expiration handler in case the task runs long.
NSAssert(self->bgTask == UIBackgroundTaskInvalid, nil);
self->bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
// Synchronize the cleanup call on the main thread in case
// the task catully finished at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (UIBackgroundTaskInvalid != self->bgTask) {
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
}
});
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// Do the work assoicated with the task.
for(int i = 0; i < 1000; i++) {
//request network.
NSLog(@"hahah %d, Time Remain = %f", i, [application backgroundTimeRemaining]);
}
// Synchronize the cleanup all on the main thread in case
// the task catully finished at around the same time.
dispatch_async(dispatch_get_main_queue(), ^{
if (UIBackgroundTaskInvalid != self->bgTask) {
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
}
});
});
}
#pragma mark -
#pragma mark Local Notifications
- (void)scheduleAlarmForDate:(NSDate *)theDate {
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if (0 < [oldNotifications count]) {
[app cancelAllLocalNotifications];
}
// Create a new notification
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"ping.caf";//@"default";
alarm.alertBody = [NSString stringWithFormat:@"Time to wake up!Now is\n[%@]",
[NSDate dateWithTimeIntervalSinceNow:10]];
[app scheduleLocalNotification:alarm];
[alarm release];
}
}
鏈夐棶棰樼暀璦錛屾垨鑰呭井鍗?http://t.sina.com.cn/passionuranus
plist鍐欏埌resouce閲岄潰鍘?/p>
閫氳繃NSBundle鎶婃暟鎹彇plist璇誨嚭鏉?/p>
2. 閫氳繃NSClassFromString鍒涘緩綾?/span>
NSClassFromString
NSSelectorFromString
姝e父鏉ヨ錛?/p>
id myObj = [[NSClassFromString(@"MySpecialClass") alloc] init];
鍜?/strong>
id myObj = [[MySpecialClass alloc] init];
鏄竴鏍風殑銆備絾鏄紝濡傛灉浣犵殑紼嬪簭涓茍涓嶅瓨鍦∕ySpecialClass榪欎釜綾伙紝涓嬮潰鐨勫啓娉曚細鍑洪敊錛岃屼笂闈㈢殑鍐欐硶鍙槸榪斿洖涓涓┖瀵硅薄鑰屽凡銆?/p>
鍥犳錛屽湪鏌愪簺鎯呭喌涓嬶紝鍙互浣跨敤NSClassFromString鏉ヨ繘琛屼綘涓嶇‘瀹氱殑綾葷殑鍒濆鍖栥?/p>
姣斿鍦╥Phone涓紝NSTask鍙兘灝變細鍑虹幇榪欑鎯呭喌錛屾墍浠ュ湪浣犻渶瑕佷嬌鐢∟STask鏃訛紝鏈濂戒嬌鐢細
[[NSClassFromString(@"NSTask") .....]]
鑰屼笉瑕佺洿鎺ヤ嬌鐢╗NSTask ...]榪欑鍐欐硶銆?/p>
NSClassFromString鐨勫ソ澶勬槸錛?/font>
1 寮卞寲榪炴帴錛?font color="#000000" style="line-height: normal; ">鍥犳騫朵笉浼氭妸娌℃湁鐨凢ramework涔焞ink鍒扮▼搴忎腑銆?/font>
2 涓嶉渶瑕佷嬌鐢╥mport錛?font color="#000000" style="line-height: normal; ">鍥犱負綾繪槸鍔ㄦ佸姞杞界殑錛屽彧瑕佸瓨鍦ㄥ氨鍙互鍔犺澆銆?/font>for (int c=0; c<[classNames count]; c++) {
NSString *className=[classNames objectAtIndex:c];
id class=[[NSClassFromString(className) alloc] init];
for (int i=0; i<[params count]; i++) {
[class performSelector:NSSelectorFromString([NSString stringWithFormat:@"setA%i",i])];
}
}
鏈夐棶棰橈紝璇風暀璦
鏂版氮寰崥錛歅assionuranus