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

2015年9月29日 星期二

簡單介紹Swift 2.0的Guard語法

Guard是iOS在Swift 2.0中引入的一種語法
他改善了一些冗長的if寫法問題(下面範例會提到)
一般來說 guard要撘配else來使用,最好的用法(官方建議)是用在方法(method)中的return / break / continue 等流程控制

Swift的guard語法非常適合用來做optional的判斷
(概念上也可以把guard直接當做 if not 來看)

以下是簡單的範例:


// userName是個optional的值,可能是使用者輸入,或是網路來的
var userName:String?

func checkUserExists(name:String?)->Bool{
// 這裡用 底線(_) 是因為我們不是真的要值,只是想判斷optional是否有值
guard let _ = userName else{
return false
}
return true
}

checkUserExists(userName) // 輸出 false
userName = "Seachaos" // 給予userName有真正的值
checkUserExists(userName) // 輸出 true



上面範列的checkUserExists就是個簡單的例子

再來看一個滿常用到的範例,就是Dictionary (不管是Swift的Dictionary還是 NSDictionary 都適用) 的處理

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年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

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