2013年12月27日 星期五

在yourApp-Prefix.pch中加入你要引用的.h檔,即可以自動引用到每一個Class中

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

#include "Define.h"    //<===加在這
#include "KSMacros.h"  //<===加在這
#include "Singleton.h" //<===加在這


#endif

2013年12月25日 星期三

可以在關閉App後仍能保存物件的NSUserDefaults


//儲存到NSUserDefaults 
-(void)saveNSUserDefaults 
NSString *myString = @"enuola"; 
int myInteger = 100; 
float myFloat = 50.0f; 
double myDouble = 20.0; 
NSDate *myDate = [NSDate date]; 
NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil]; 
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", nil]]; 

//將上面的物件儲存到NSUserDefaults中 
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
//除NSNumber需用對應的型別以外,其他的都是使用setObject:forKey: 
[userDefaults setInteger:myInteger forKey:@"myInteger"]; 
[userDefaults setFloat:myFloat forKey:@"myFloat"]; 
[userDefaults setDouble:myDouble forKey:@"myDouble"]; 

[userDefaults setObject:myString forKey:@"myString"]; 
[userDefaults setObject:myDate forKey:@"myDate"]; 
[userDefaults setObject:myArray forKey:@"myArray"]; 
[userDefaults setObject:myDictionary forKey:@"myDictionary"]; 

//建議同步,但是不是必須的 
[userDefaults synchronize]; 


//從NSUserDefaults讀取資料 
-(void)readNSUserDefaults 
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults]; 

//讀取數據到各個label中 
//讀取整型int類型的數據 
NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"]; 
txtInteger.text = [NSString stringWithFormat:@"%d",myInteger]; 

//讀取浮點型float類型的數據 
float myFloat = [userDefaultes floatForKey:@"myFloat"]; 
txtFloat.text = [NSString stringWithFormat:@"%f",myFloat]; 

//讀取double類型的數據 
double myDouble = [userDefaultes doubleForKey:@"myDouble"]; 
txtDouble.text = [NSString stringWithFormat:@"%f",myDouble]; 

//讀取NSString類型的數據 
NSString *myString = [userDefaultes stringForKey:@"myString"]; 
txtNSString.text = myString; 

//讀取NSDate日期類型的數據 
NSDate *myDate = [userDefaultes valueForKey:@"myDate"]; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
txtNSDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]]; 

//讀取數組NSArray類型的數據 
NSArray *myArray = [userDefaultes arrayForKey:@"myArray"]; 
NSString *myArrayString = [[NSString alloc] init]; 
for(NSString *str in myArray) 
NSLog(@"str= %@",str); 
myArrayString = [NSString stringWithFormat:@"%@ %@", myArrayString, str]; 
[myArrayString stringByAppendingString:str]; 
// [myArrayString stringByAppendingFormat:@"%@",str]; 
NSLog(@"myArrayString=%@",myArrayString); 
txtNSArray.text = myArrayString; 

//讀取字典類型NSDictionary類型的數據 
NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"]; 
NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]]; 
txtNSDictionary.text = myDicString; 
}

在block中要使用self的正確方式

//在進去之前要把它設為weak,讓self消失時,block中的self也跟著消失
//在進之之後要把它設為strong,讓self在block執行時,不會自行消失
__weak typeof(self) weakSelf = self;
    [_bleManager setReadingValueCompletion:^(NSData *responseData) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        strongSelf.showResult.text = [NSString stringWithFormat:@"%@",responseData];
        [strongSelf.receivedData appendData:responseData];

    }];

//在swift中使用
weak let weakSelf = self
或者在closure開頭加上[weak self]
在closure裡就直接使用self?
例如
dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in
            self?.chatMessageTableView.reloadData()

        })

2013年12月24日 星期二

從某字串以後重新抓取字串

NSRange search = [value rangeOfString:@"#*s$"];
value = [value substringFromIndex:search.location];

直接宣告為singleton的.h

到時只要import這個.h,就可以用這個方法宣告

#define SYNTHESIZE_SINGLETON_FOR_HEADER(className) \
\
+ (className *)shared##className;

#define SYNTHESIZE_SINGLETON_FOR_CLASS(className) \
\
+ (className *)shared##className { \
static className *shared##className = nil; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
shared##className = [[self alloc] init]; \
}); \
return shared##className; \

}

iOS7似乎是little-endian

因為我一開始想對<dd07>做swap之後再印出數值,結果是65XXX多少的,後來把swap拿掉,就印出我真正想要的數字2013

//以下為程式碼
    NSData *myData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Test" ofType:@"bin"]];
    
    NSLog(@"myData:%@",myData);
    NSRange range = NSMakeRange(0, 2);
    NSLog(@"year data:%@",[myData subdataWithRange:range].description);
    NSData* yearData = [myData subdataWithRange:range];
    unsigned short x;
    [yearData getBytes:&x length:sizeof(x)];
    NSLog(@"x:%x",x);
    //unsigned short year = [self swap:x];
    NSLog(@"year:%hu",x);

year data:<dd07>
x:7dd
year:2013

-(UInt16) swap:(UInt16)s {
    NSLog(@"swap");
    UInt16 temp = s << 8;
    NSLog(@"%x",temp);
    temp |= (s >> 8);
    NSLog(@"%x",temp);
    return temp;
}

//20140207發現
iOS7在寫入時,不會自動換成little-endian
所以結論是,
讀取時,是以little-endian的方式直接讀取data
寫入時,是以data的格式直接寫入,所以要先swap成little-endian的排列,再寫入。

用NSData直接操作binary data

//下列這五個是你會用到的NSData方法
//將另一個NSData加到原來的NSData後面
-(void)appendData:(NSData*)otherData

//將bytes直接加入NSData,參數為1.bytes的起始位置(要加&),2.bytes的長度
-(void)appendBytes:(const void*)bytes length:(NSUInteger)length

//截取其中某一段NSData
-(NSData*)subdataWithRange:(NSRange)range

//從你的NSData中copy某段bytes出來,存到你新建的buffer(要加&)中(存數值資料,而非NSData)
-(void)getBytes:(void*)buffer range:(NSRange)range

//從你的NSData中從頭copy某段指定長度的bytes出來,存到你新建的buffer(要加&)中(存數值資料,而非NSData)
-(void)getBytes:(void*)buffer length:(NSUInteger)length

//底下是一些範例
//直接取得binary檔案
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:@"yourBinary" withExtension:@"bin"];
        NSString *stringPath = [imgPath absoluteString];

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];

//要把NSData轉回NSNumber時(這裡是以short為範例)
  short s;
  [yourData getBytes:&s range:range];
  NSNumber* stepCount = [NSNumber numberWithUnsignedShort:s];


// low level read method - read data while there is data and space available in the input buffer
- (void)_readData
{
    uint8_t buf[EAD_INPUT_BUFFER_SIZE];
    while ([[_session inputStream] hasBytesAvailable])
    {
        NSInteger bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];
        if (_readData == nil)
        {
            _readData = [[NSMutableData alloc] init];
        }
        [_readData appendBytes:(void *)buf length:bytesRead];
        //NSLog(@"read %d bytes from input stream", bytesRead);
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:EADSessionDataReceivedNotification object:self userInfo:nil];

    //清空_readData
    if( _readData )
    {
        [_readData setLength:0];
    }
}


// high level read method
// 這裡輸入的參數為你想要切的長度。
- (NSData *)readData:(NSUInteger)bytesToRead
{
    NSData *data = nil;
    if ([_readData length] >= bytesToRead)
    {
        NSRange range = NSMakeRange(0, bytesToRead);
        data = [_readData subdataWithRange:range];
        [_readData replaceBytesInRange:range withBytes:NULL length:0];
    }
    return data;
}

// get number of bytes read into local buffer

-(NSUInteger)readBytesAvailable
{
    return [_readData length];

}

//自己寫的切封包
+ (NSArray*)splitDataIntoChunks:(NSData*)data size:(NSUInteger)bytesPerChunk{
    
    if(!data) return nil;
    
    NSMutableArray* array = [[NSMutableArray alloc]init];
    NSUInteger dataLength = [data length];
    NSUInteger chunkCount = 0;
    while (chunkCount < dataLength)
    {
        NSRange range = NSMakeRange(chunkCount, bytesPerChunk);
        NSData* chunk = [data subdataWithRange:range];
        [array addObject:chunk];
        chunkCount += 2;
    }

    return array;

}

2013年12月23日 星期一

取得檔案的二種方法nsbundle & nsfilehandle

NSString *newPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];

NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:newPath];

[[NSBundle mainBundle] pathForResource:@"test" ofType:@"jat"];

2013年12月20日 星期五

EAAccessory的Properties代表的資訊

//這是連上Device時,iOS指定的一個連線ID,斷線之後就不存在
//如果有二台以上同型的Device,你可以用connectionID來分別它們
_accessory.connectionID : 23431639
//Device的製造商名稱
_accessory.manufacturer : Acme
//Device的顯示名稱
_accessory.name : iAP Auth Only
//Device的模組資訊
_accessory.modelNumber : Acme 3.0
//Device的出廠序號
_accessory.serialNumber : 2.71828182
//Device的F/W版本
_accessory.firmwareRevision : 3.0.0
//Device的硬體版本
_accessory.hardwareRevision : 4.1.0

//由下列source code讀出
-(void)printProperties
{
    NSLog(@"_accessory.connectionID : %i", _accessory.connectionID);
    NSLog(@"_accessory.manufacturer : %@", _accessory.manufacturer);
 
    NSLog(@"_accessory.name : %@", _accessory.name);
    NSLog(@"_accessory.modelNumber : %@", _accessory.modelNumber);
    NSLog(@"_accessory.serialNumber : %@", _accessory.serialNumber);
    NSLog(@"_accessory.firmwareRevision : %@", _accessory.firmwareRevision);
    NSLog(@"_accessory.hardwareRevision : %@", _accessory.hardwareRevision);
 
}

2013年12月19日 星期四

c語言中的波折號

是一個位元運算符號

是將某值換成位元之後,取not值(即0->1;1->0)

解決capturing self strongly in this block is likely to lead to a retain cycle的問題

__weak typeof(self) weakSelf = self;
[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
                                     queue:nil
                                usingBlock:^(CMTime time) {
                                                current+=1;

                                                if(current==60)
                                                {
                                                    min+=(current/60);
                                                    current = 0;
                                                }

                                                 [weakSelf.timerDisp setText:[NSString stringWithFormat:@"%02d:%02d",min,current]];
                                            }];

較嚴格的singleton寫法(封住init)

//利用dispatch_once來寫,會比@synchronized(self)快上三倍,但如果instance被消除,前言不會重新init一次,但外部本來就無法消除內部的instance,所以小心不要在內部消除即可
//自己的範例
#pragma mark - Initial with Singleton
+(instancetype) sharedInstance {
    static dispatch_once_t pred;
    static CBManager *instance = nil;
    dispatch_once(&pred, ^{instance = [[self alloc] initSingleton];});
    return instance;
}
- (id)init {
    return nil;
}
- (id)initSingleton {
    self = [super init];
    if ( self ) {
        //TODO: ...
    }
    return self;
}

//網路上的範例
+ (MYSingleton *)sharedSingleton {
     static dispatch_once_t pred;
     static MYSingleton *instance = nil;
     dispatch_once(&pred, ^{instance = [[self alloc] initSingleton];});
     return instance;
}
- (id)init {
    // Forbid calls to –init or +new
    NSAssert(NO, @”Cannot create instance of Singleton);
    // You can return nil or [self initSingleton] here, 
    // depending on how you prefer to fail.
    return nil;
}
// Real (private) init method
- (id)initSingleton {
    self = [super init];
    if ((self = [super init])) {
    // Init code }
    return self;
}

2013年12月17日 星期二

External Accessory Framework連線

//方法一   跳出手動點選的連線視窗
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS 'MyString'"];

[[EAAccessoryManager sharedAccessoryManager] showBluetoothAccessoryPickerWithNameFilter:predicate completion:nil]


//方法二   程式回連
//先用這個取回現正連線的裝置,他會回傳NSArray
[[EAAccessoryManager sharedAccessoryManager]connectedAccessories ];
//然後在Array中取出你想連線的裝置
//最後用這個去打開session即可使用
_session = [[EASession alloc] initWithAccessory:_accessory forProtocol:MFI_BUNDLE_ID];

消鍵盤的三種方式

//一次把view上面的所有text field的鍵盤都消完
[self.view endEditing:YES];

//針對某個text field去消鍵盤
[self.textBox resignFirstResponder];

//如果要用鍵盤上return key的方式,要去實作textFiled的did end on exit的方法
再在方法中加入以上消鍵盤的方法即可
//或者在離開textField的時候實作
- (IBAction)TextField_DidEndOnExit:(id)sender {
    [sender resignFirstResponder];
}

P.S.消鍵盤的設定,都要在main thread完成,不然會導致app crash.
*** Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit/UIKit-2935.138/Keyboard/UIKeyboardTaskQueue.m:368
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

2013年12月16日 星期一

抓iPhone的手機名稱

NSString* deviceName = [[NSString alloc]init];
deviceName = [[UIDevice currentDevice] name];

2013年12月13日 星期五

取絕對值

//取整數的絕對值
abs(int i);

//取float的絕對值
fabsf(float i);

//取double的絕對值
fabs(double i);

2013年12月12日 星期四

UIAlertView的使用

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"didUpdateToLocation"//設定標題
message:[NSString stringWithFormat:@"%f",_heading.trueHeading ]  //設定警告訊息內文
                                                   delegate:self // 要用那一個物件來處理按鈕的delegate事件
                          
cancelButtonTitle:@"OK"  //cancel按鈕文字的設定
otherButtonTitles: nil]; // 設定其他按鈕title
// 如果要多個其他按鈕 >> otherButtonTitles: @"check1", @"check2", nil];
    
    [alert show];  // alert這個物件秀出來
          alert.tag = 0; //當一個View有多個alertView時,可以用來區分
    [alert release];  // 把物件釋放(MRC環境下)
}



//2秒後關alert view
double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [alert dismissWithClickedButtonIndex:0 animated:NO];

    });


//包成method
-(void)_showAlertViewWithTitle:(NSString*)title content:(NSString*)content showTime:(float)showTime{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:content
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"  //cancel按鈕文字的設定
                                          otherButtonTitles: nil]; // 設定其他按鈕title
    // 如果要多個其他按鈕 >> otherButtonTitles: @"check1", @"check2", nil];
    [alert setTag:0];
    [alert show];  // alert這個物件秀出來
    
    //2秒後關alert view
    double delayInSeconds = showTime;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (showTime != 0.0) {
            [alert dismissWithClickedButtonIndex:0 animated:NO];
        }
    });

}


#pragma marks -- UIAlertViewDelegate --
//用Delegate實現alertView的按鍵功能
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickButtonAtIndex:%d",buttonIndex);
}

//AlertView已經消失時的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"didDismissWithButtonIndex");
}

//ALertView即將消失時的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"willDismissWithButtonIndex");
}

//AlertView的取消按鍵的事件
-(void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"alertViewCancel");
}

//AlertView已經顯示時的事件
-(void)didPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"didPresentAlertView");
}

//AlertView即將顯示時的事件
-(void)willPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"willPresentAlertView");
}  

2013年12月11日 星期三

抓最新位置的機制

1,每次更新時,觸發delegate,把最近位置放到Add到Array的最後一個
2,再用lastObject取出最新的location

2013年12月9日 星期一

如果NSMutableArray裡沒有就加入

//如果NSMutableArray裡沒有就加入
if(![self.dicoveredPeripherals containsObject:peripheral])
        [self.dicoveredPeripherals addObject:peripheral];


//如果NSMutableArray裡有就移除
if([self.dicoveredPeripherals containsObject:peripheral])

        [self.dicoveredPeripherals removeObject:peripheral];

new = alloc + init

self.characteristicsCBUUID = [NSMutableDictionary new];

等於

self.characteristicsCBUUID = [[NSMutableDictionary alloc]init];

模仿官方版的註解格式,就可以在右邊的quick helper看到註解

手寫的註解
/*!
 *  @method swap:
 *
 *  @param s Uint16 value to byteswap
 *
 *  @discussion swap byteswaps a UInt16 
 *
 *  @return Byteswapped UInt16
 */

-(UInt16) swap:(UInt16)s {
    UInt16 temp = s << 8;
    temp |= (s >> 8);
    return temp;

}


官方版的註解
/*!
 *  @method writeValue:forCharacteristic:type:
 *
 *  @param data The value to write.
 *  @param characteristic The characteristic whose characteristic value will be written.
 *  @param type The type of write to be executed.
 *
 *  @discussion Writes <i>value</i> to <i>characteristic</i>'s characteristic value. If the <code>CBCharacteristicWriteWithResponse</code>
 * type is specified, {@link peripheral:didWriteValueForCharacteristic:error:} is called with the result of the write request.
 *
 *  @see peripheral:didWriteValueForCharacteristic:error:
 * @see CBCharacteristicWriteType
 */
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

override "init" in objective-c

- (id)init {
    self = [super init];
    if (self) {
        [self resetCentralManager];
    }
    return self;

}

-(void)resetCentralManager{
    // Will callack on centralManagerDidUpdateState delegate
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    _data = [[NSMutableData alloc] init];
}

2013年12月8日 星期日

init與initialize的分別

initialize是在你初始化時,對你的class先做初始化

init是你對class做完初始化之後,對你的instance做初始化

所以我們對同一個class建立三個instance時

initialize只會在一開始被執行一次,init會在之後被執行三次

2013年12月5日 星期四

去除註解(利用正則表示式)

到xcode左邊,選搜尋的地方,選正則表示式,再取代成空值

去範圍註解
/\*.*[\*][\/]


去單行註解
//.*$

2013年12月3日 星期二

判斷iOS7

+(BOOL)isIOS7
{
    return ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f);

}

縮放圖片

+(UIImage *)scaleImage:(UIImage *)_image size:(CGSize)_size
{
    CGFloat _toWidth  = _size.width;
    CGFloat _toHeight = _size.height;
    float _x = 0.0f;
    float _y = 0.0f;
    CGRect _frame = CGRectMake(_x, _y, _toWidth, _toHeight);
    float _oldWidth  = _image.size.width;
    float _oldHeight = _image.size.height;
    float _scaleRatio   = MAX( (_toWidth / _oldWidth), (_toHeight / _oldHeight) );
    float _equalWidth   = (int)( _oldWidth * _scaleRatio );
    float _equalHeight  = (int)( _oldHeight * _scaleRatio );
    UIGraphicsBeginImageContext( CGSizeMake(_equalWidth, _equalHeight) );
    [_image drawInRect:CGRectMake(0, 0, _equalWidth, _equalHeight)];
    _image = nil;
    _image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    _x = floor( (_equalWidth -  _toWidth) / 2 );
    _y = floor( (_equalHeight - _toHeight) / 2 );
    _frame = CGRectMake(_x, _y, _toWidth, _toHeight);
    CGImageRef _smallImage = CGImageCreateWithImageInRect( [_image CGImage], _frame );
    UIImage *_doneImage    = [UIImage imageWithCGImage:_smallImage];
    CGImageRelease(_smallImage);
    return _doneImage;
}

2013年12月2日 星期一

寫個timer來用

#pragma mark- Update by Timer Trigger
//對外接口
-(void) switchTransferDataTimerStatus:(BOOL)doYouWannaTurnOnTimer{
    if (doYouWannaTurnOnTimer) {
        [self _startTransferTimer];
    }else{
        [self _stopTransferTimer];
    }
}
//開始timer
-(void)_startTransferTimer
{
    if( !_transferTimer )
    {
        [self performSelector:@selector(_transferSportData) withObject:nil afterDelay:0.8f];
        _transferTimer = [NSTimer scheduledTimerWithTimeInterval:TranferToWatchCycleInterval
                                                               target:self
                                                             selector:@selector(_transferSportData)
                                                             userInfo:nil
                                                              repeats:YES];
    }
}
//關掉timer
-(void)_stopTransferTimer
{
    if( _transferTimer )
    {
        [_transferTimer invalidate];
        _transferTimer = nil;
    }
}

//你要用timer做的事
-(void)_transferSportData{
    
    NSLog(@"我被印出來啦");

}