也不能選擇模擬器
只有My Mac 64-bit 可以執行
但是按了Run一樣沒反應
這時候只要點專案的PROJECT->Build Settings->Base SDK這邊修改為Xcode有的SDK
這時候再去Run大致上都可以執行了

UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
bgColorView.layer.cornerRadius = 10;
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
UIImage *selectionBackground = [UIImage imageNamed:@"yellow_bar.png"];
UIImageView *iview=[[UIImageView alloc] initWithImage:selectionBackground];
self.selectedBackgroundView=iview;
}
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
player.volume = 1.0;
[player play];
+ (id)sharedInstance
{
static id sharedInstance = nil;
if (!sharedInstance) {
sharedInstance = [[NSObject alloc] init];
}
return sharedInstance;
}
+ (id)sharedInstance
{
static id sharedInstance = nil;
static BOOL token = NO;
if (!token) {
token = YES;
sharedInstance = [[NSObject alloc] init];
}
return sharedInstance;
}
但是参考了苹果官方的单例模式代码,发现它看起来是这样的:
+ (id)sharedInstance
{
static id sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[NSObject alloc] init];
});
return sharedInstance;
}
那么它们究竟有多大区别呢?
+(id)sharedInstance
{
static dispatch_once_t pred;
static MyClass *sharedInstance = nil;
dispatch_once(&pred, ^{
sharedInstance = [[MyClass alloc] init];
});
return sharedInstance;
}
- (void)dealloc
{
// implement -dealloc & remove abort() when refactoring for
// non-singleton use.
abort();
}
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MyClass alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance = nil;
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}
}
return sharedInstance;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *fileFolder = [documentsPath stringByAppendingPathComponent:@"myRecord"];
// 如果資料夾不存在
if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]){
NSError* error;
if([[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error])
{
NSLog(@"success");
}
else
{
NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]);
NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
}
}
else
{
// 資料夾存在
}
NSError* error;
[[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:&error]
// 你得先宣告一個 NSUserDefaults 物件,在同一個 app 裡,它是 singleton(單例)
NSUserDefaults *userPrefs = [NSUserDefaults standardUserDefaults];
// set
[userPrefs setObject:@"a123456789" forKey:@"userID"];
[userPrefs setInteger:24 forKey:@"age"];
[userPrefs setBool:YES forKey:@"isLogin"];
// remove
[userPrefs removeObjectForKey:@"debts"];
[userPrefs synchronize];
// get
NSString *userID = [userPrefs stringForKey:@"userID"];
BOOL isLogin = [userPrefs boolForKey:@"isLogin"];
要注意的是,set 或 remove 之後,記得執行 [userPrefs synchronize],已確保資料被寫入硬碟裡。再保險一點,你還可以:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSString *fileName = @"test";
NSURL *url = [[NSURL alloc] initFileURLWithPath:
[[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"] isDirectory:NO];
NSError* error = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.delegate = self;
if (!url || error) {
}
player.volume = 1;
player.numberOfLoops = -1;// -1 = repeat forever
[player prepareToPlay];
[player play];
pause
if([player isPlaying])
{
[player pause];
player = nil;
}
#pragma mark -
#pragma mark AVAudioPlayer delegate
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)aPlayer successfully: (BOOL)flag
{
if (flag == YES) {
NSLog(@"audioPlayerDidFinishPlaying");
}
}