2018年6月28日 星期四

[array copy] 和 [NSArray arrayWithArray:array] 的異同

// https://segmentfault.com/q/1010000003854902
如果arrayimmutable类型的NSArray
  • copy只是retain,没有创建新对象
  • arrayWithArray 创建了新的NSArray对象,并将原有数组元素填充进去,数组元素还是原来的对象
如果arraymutable类型的NSArray
  • 二者最终结果是等效的,创建了新的NSArray对象,但数据元素还是原来的对象
如果array == nil
  • copy的结果是nil
  • arrayWithArray 结果是长度为0的NSArray对象
无论哪种情况,数组元素都是共享的 不会被复制

2018年6月12日 星期二

nslog to file

- (void)redirectLogToDocuments {
    NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [allPaths objectAtIndex:0];
    NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"logFile.txt"];
  
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathForLog]) {
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtPath:pathForLog error:&error];
        if (error!=nil) {
            NSLog(@"%@", error.localizedDescription);
        }
    }
    freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

讓圖示在AttributedText上下置中


https://stackoverflow.com/questions/26105803/center-nstextattachment-image-next-to-single-line-uilabel




let iconImage = UIImage(named: "icon.png")!
var icon = NSTextAttachment()
icon.bounds = CGRect(x: 0, y: (titleFont.capHeight - iconImage.size.height).rounded() / 2, width: iconImage.size.width, height: iconImage.size.height)
icon.image = iconImage
let iconString = NSAttributedString(attachment: icon)
titleText.append(iconString)

2018年6月8日 星期五

rate the app

- (IBAction)rateButtonTapped:(UIButton *)sender {
    if (@available(iOS 10.3, *)) {
        [SKStoreReviewController requestReview];
    } else {
        NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/tw/app/swipedon-visitor-management/id986185962?l=en&mt=8"];
        [[UIApplication sharedApplication] openURL:url];
    }
}

2018年6月5日 星期二

用CoreData存圖片

不要用core data存圖
是可以轉存NSData去存,但只要你的ManagedObjectContext沒有釋放,那麼那張圖就不會被釋放,如果圖片大一點,一下就爆了

用core data存imageName,把圖片存到文件資料夾裡,用imageName去對應,
並且監聽imageName的setter,在改名之前先刪去原來的圖。