顯示具有 IOS 標籤的文章。 顯示所有文章
顯示具有 IOS 標籤的文章。 顯示所有文章

2016年6月26日 星期日

iOS 模擬器 couldn't be completed. (LaunchServicesError ... 解法



如果有使用 CocoaPods 的 XCode 專案,有時候會遇到一些鬼打牆的問題
例如 無緣無故忽然間就出現什麼 /bin/sh exit code 1
或是

The operation couldn't be completed. (LaunchServicesError error 0.)



Command /bin/sh failed with exit code 1

還是不能 copy 什麼 framework,出現 exit code 之類的

都有可能是專案沒有 Clean 乾淨 ( 一般的 Product -> Clean 可能無效)
這時候要按 Alt ,再選一次 Product -> Clean Build Folder ( 隱藏功能)
就有可能變正常了


2015年9月22日 星期二

iOS測量文字長度, 以Swift和Objective-C為例

iOS中,除了用UILabel的SizeToFit來測量文字長度外
NSString其實也有內建提供這個方法

以下是Objective-C的範例:
+ (CGSize)getSizeFromString:(NSString*)str withFont:(UIFont*)font{

    return [str sizeWithAttributes:@{

                                     NSFontAttributeName : font

                                     }];

}


這個method只要輸入NSString和UIFont,就可以得到String的尺吋(以CGSize回傳,有該String將要顯示的width或是height)

而Swift要測量的話,要先轉成NSString,且AttributteName也有所修改
如下
func getSizeFromString(string:String, withFont font:UIFont)->CGSize{

        let textSize = NSString(string: string ?? "").sizeWithAttributes(

            [ NSFontAttributeName:font ])

        return textSize

}

2015年9月20日 星期日

XCode 7 遇到 Bitcode 錯誤的解決辦法


最近Apple Release了 XCode 7
如果在開發iOS的APP 有遇到類以下的錯誤:



ld: '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/usr/lib/dylib1.o' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

那就是遇到了BITCODE的問題了

解決辦法很簡單,就是關掉他,方法如下圖
(到專案的 Build Settings ,搜尋 bitcode)
BitCode是什麼呢?

他是一個有點像是Java Bytecode一樣的東西
但他是給LLVM (Apple的編譯器)用的,簡單的說就是Apple希望你可以在上傳到App Store時也一起上傳Bitcode,好處就是App Store可以幫你針對不同的 iOS 設備去優化你的App,不用重新上傳或是針對CPU做編譯

但會出現以上的錯誤可能就是因為用了沒有Bitcode的Library(通常是C++或是第三方的套件)
所以XCode在產生Bitcode就會失敗了
目前看來Bitcode比較適用於純 Swift 或是Objective-C的專案



2015年8月17日 星期一

iOS 鍵盤事件 - 使用NSNotificationCenter



iOS中有NSNotificationCenter可以接收Notify
其中鍵盤彈出事件是滿常用到的,例如要控制UIScrollView的範圍和大小

以鍵盤事件的範例如下:

可以在viewWillAppear中註冊Notification
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
 

2015年3月6日 星期五

IOS-Swift 在Swift中使用substring 切割 String

在Swift中如果要切割一個String比較麻煩,不能直接給他int 型態的初始值
拿 substringToIndex 來說
它是從第一個字元開始切

let str = "abcdefg";
let endIndex = advance(str.startIndex, 1); //取得第一個字元 Index
let firstChar = str.substringToIndex(endIndex);

firstChar 就會等於 "a" 

如果此篇對您有幫助,您可以點選廣告給予最大的動力,感謝您的收看。

2015年1月22日 星期四

IOS-Swift 時間轉換(Uxin time to Date)

let dateFormatter = NSDateFormatter(); //初始化
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss"; //指定格式化格式
dateFormatter.timeZone = NSTimeZone.localTimeZone(); 
let timeNum:NSNumber = timeStr as NSNumber;  //將字串轉為NSNumber

let date = NSDate(timeIntervalSince1970: timeNum.doubleValue/1000);  //將number 轉換為double 再轉換為 date
dateFormatter.stringFromDate(date) //date 轉為 string

timeStr = 1413444597503.0

如果此篇對您有幫助,您可以點選廣告給予最大的動力,感謝您的收看。