一般自定常數的方法有兩種,
1. 在header(.h)檔
FOUNDATION_EXPORT NSString * const MY_NAME;
FOUNDATION_EXPORT NSString * const MY_GIRLFRIEND_NAME;
於.m檔
NSString * const MY_NAME = @"James";
NSString * const MY_GIRLFRIEND_NAME = @"Stella";
2. 另一種定義常數的方法為:
#define MY_NAM @"James"
#define MY_GIRLFRIEND_NAME @"Stella"
這兩種方法在檢查字串時,第一種可以使用(StringInstance == MY_NAME )來比較,
而使用define則是使用([StringInstance isEqualToString:MY_NAME]) 來比較。
第一種直接比較是使用pointer address,第二種比較則是比較每個字元是否相等。
2015年2月12日 星期四
2015年1月26日 星期一
gen_entitlements.py: No such file or directory實機報錯
今天要把別的廠商的專案進行測試
但拿到專案檔一直無法傳到手機上,
以為是憑證哪出了問題,但後來發現不是!
錯誤訊息部份如下
line 4: /Applications/Xcode.app/Contents/Developer/iphoneentitlements/gen_entitlements.py: No such file or directory
但拿到專案檔一直無法傳到手機上,
以為是憑證哪出了問題,但後來發現不是!
錯誤訊息部份如下
line 4: /Applications/Xcode.app/Contents/Developer/iphoneentitlements/gen_entitlements.py: No such file or directory
後來發現這個是之前公司在作假憑證的run script出了問題
只要到build phase裡刪除 run script
再使用正確的debug憑證去發佈至手機即可
2014年12月9日 星期二
啟用Watch App Simulator
目前測試 apple 最新的 apple watch 可以透過XCode 6.2 beta版
apple watch的project是要綁在手機上的專案,
所以watch app其實只是手機app的延伸,
若你今天要啟用apple watch simulator的話,
只要在xcode的target選擇watch app
然後按下compile
模擬器就會跑出來囉!
若你發現只有手機模擬器出現的話,
你可以在模擬器的工具列
Hardware→External Displays→選到最下方的Apple Watch
這樣Apple watch的simulator就會跑出來囉!
apple watch的project是要綁在手機上的專案,
所以watch app其實只是手機app的延伸,
若你今天要啟用apple watch simulator的話,
只要在xcode的target選擇watch app
然後按下compile
模擬器就會跑出來囉!
若你發現只有手機模擬器出現的話,
你可以在模擬器的工具列
Hardware→External Displays→選到最下方的Apple Watch
這樣Apple watch的simulator就會跑出來囉!
2014年11月25日 星期二
git SSL certificate problem: self signed certificate
今天同事使用Git 發生了
Git Pull Failed: fatal: unable to access 'https://git.xxxx.xxx.xx.xxx.git/': SSL certificate problem: self signed certificate
的問題
去你的Repoistory的.git內執行
詳情可以參考
http://stackoverflow.com/questions/11621768/how-can-i-make-git-accept-a-self-signed-certificate
Git Pull Failed: fatal: unable to access 'https://git.xxxx.xxx.xx.xxx.git/': SSL certificate problem: self signed certificate
的問題
去你的Repoistory的.git內執行
git config http.sslVerify false
詳情可以參考
http://stackoverflow.com/questions/11621768/how-can-i-make-git-accept-a-self-signed-certificate
2014年11月18日 星期二
Git "....java did not match any file(s) known to git..."問題
今天使用IntelliJ IDEA的Git pull 與 push時,
一直出現以下的錯誤
苦惱了一個上午,
最後找到原因是因為IDEA的IDE透逗怪怪的,
只要把該檔案做commit 但不要用IDE去做
去terminal下指令
再用IDE或指令去pull or push
這樣就可以正常囉
一直出現以下的錯誤
Error:error: pathspec 'src/com/aaaa/bbbb/cccc/dddd/DMF.java' did not match any file(s) known to git.
during executing git commit --only -F /private/var/folders/6h/zv5mh4px15d6yjksw693q_s40000gp/T/git-commit-msg-5225757047314811222.txt -- src/com/aaaa/bbbb/cccc/dddd/DMF.java
苦惱了一個上午,
最後找到原因是因為IDEA的IDE透逗怪怪的,
只要把該檔案做commit 但不要用IDE去做
去terminal下指令
git commit -m commitMessage
再用IDE或指令去pull or push
這樣就可以正常囉
2014年9月16日 星期二
Swift 筆記[ 2014.10.06更新]
1. 語句不強制加;
2. 可不用宣告明確的型別,var用來宣告變數,let用來宣告常數,
若要宣告明確的型別可以下寫法:
let explicitDouble:Double = 70
3. 變數無法直接做隱式轉換,若想要轉換變數到不同的型態,
需明確指定要轉什麼型態。
ex:
let label = "this is label"
let width = 99
let labelWidth = label + String(width)
4. 以上的寫法之外,可以用更簡易的方式來轉成字串
ex:
let apple = 3
let oranges = 5
let appleSum = "I have \(apple) apples."
let fruitSummary = "I have \(apple + oranges) pieces of fruit."
5. 建立array的方式與一般程式語言一樣,利用[] 與index來存取
ex:
var shoppingList =["fish", "water", "fruit"]
shoppingList[1] = "bottle of water"
ex:
var occupation = [
"Malcolm":"Captain",
"Kaylee":"Mechanic",
]
occupation["Jayne"] = "Public Relations"
6. 建立空的array與dictionary 利用以下初始化語法:
let emptyAry = [String]()
let emptyDictionary = [String: Float]()
7. 如果型態資訊能夠被推論,你可以寫一個空的array如[] 和空的dictionary如[:],
利如你設立一個新的值給變數,或傳一個參數到function
ex:
shppingList = []
occupations=[:]
8. 與一般程式語言不同,if else 與 for-in , for, while, do-while不用加左右括號()
ex:
let individualScores=[9,7,5,3,1,2,4,6,8]
var teamScore = 0
for score in individualScores{
if score > 4{
teamScore+=3
}
else
{
teamScore+=1
}
}
teamScore
註:直接寫teamScore是在playground內直接看到內容值的方式
9. 在 if 敘述句內,這個敘對敘述句必需是Boolean描述,
像 if score {...}是錯誤的描述
10. swift整數分為8,16,32,64 位元的有號與無號整數型別,如
UInt8、Int32
11. 取得整數最小值與最大值範圍,可用 min與max屬性取得
ex:
let minValue = UInt8.min
let maxValue = UInt8.max
12. 通常你不需特別去選擇整數的size在你的程式碼,swfit提供Int,這個型態
的常度會自已對應與你現在的平台整數長度相同。即使在32Bit的平台下,Int
也能夠儲存 -2,147,483,648 ~ 2,147,483,647的範圍。
13. UInt 也另可分為UInt32、UInt64,長度也會依目前的平台對應。在swift非必要
盡量不要使用UInt,除非你真的需要目前平台的無號整數的長度。除了這個情況,
最好使用Int,即使你知道這是非負整數,統一使用Int可提高程式碼的重用性,避免
不同型別轉換,與匹配int type的推斷。
14. 浮點數範圍比Int更大。浮點數分為:Double、Float。
Double:代表64 bit浮點數。當你需要儲存很大或很高精度的浮點數。
Float:代表32 bit浮點數。精度要求不高可以使用此型別。
Double至少有15位數,而Float至少有6位數,選擇哪個型別取決於你的程式碼處理
範圍。
15. swift 是type safe的語言,會依你的變數型別去type checks。並且提供
type inference(型別推斷)。有了type inference就很少需要去宣告型別。
常數與變數雖需要明亮的型別,但大部份不需要由開發者自已完成。
例如
let meaningOfLife = 42。
這指meaningOfLife為Int型別
let pi = 3.14159
這指pi自已為Double型別。通常swift浮點數在推斷型別時,會選擇Double而非Float。
let anotherPi = 3 + 0.14159
此型別則為Double,因為3沒有明確指出是什麼型別。
16. 整數可以寫成
十進位:沒有前綴。
ex: let decimalInteger = 17
二進位:前綴是0b
ex: let binaryInteger = 0b1001 // 表示17
八進位:前綴是0o
ex: let octalInteger = 0o21 // 表示17
十六進位:前綴是0x
ex: let hexadecimalInteger = 0x11 // 表示17
17. 浮點字面可以是十進位或16進位(0x),小數點兩邊至少有一個十進位或十六進位。
浮點數有一個optional的指數(exponent),在十進位浮點數透過大寫或小寫的e來指定。
十六進位則透過p來指定。
ex:
1.25e2表示1.25x10^2等於125.0
1.25e-2表示
16進位指數表示:
ex:
0xFp2 表示 15x2^2 等於60.0
0xFp-2 表示 15x2^-2 等於3.75
18. 數值和浮點數可以添加額外的零或底線,不會影響數值。
ex:
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion=1_000_000.000_000_1
19. 通常即使知道整數變數或常數是已知的非負數,也請用Int型別。
20. 不同型別的整數可以儲存不同的範圍。
Int8:可儲存範圍 -128~127
UInt8: 可儲存 0~255
ex:
let cannotBeNegative: UInt8 = -1
// 會報error 不能存負數
let tooBig:Int8 = Int8.max +1
// 超過最大數值會報錯
21. 每個整數可儲存不同範圍值,必須在程式依不同情況選擇數值型型別轉換,
可預防隱式轉換的錯誤。
22. 以下例子twoThousand是 UInt16 type,而one是 UInt8 type,兩者不能相加,
需將one轉換成UInt16才可相加。
ex:
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
twoThousandAndOne則被推斷為UInt16的type,因為兩個UInt16 type值相加。
23. SomeType(ofInitialValue)是呼叫UInt16一個初始化的函式,可接受UInt8的型別值。
這個初始化可以讓現有的UInt8創建一個新的UInt16 type。
24. 整數與浮點數必須做顯示指定型別轉換。
ex:
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
25. 浮點數轉整數。
ex:
let integerPi = Int(pi)
26. 可以利用typealias來定義型別別名
ex:
typealias AudioSample = UInt16
var maxAmplitudeFoud = AudioSample.min
// maxAmplitudeFoud 現在為0
AudioSample就是UInt16
27. Swift的布林型態為Bool ,供兩種值分別為 true , false。與Int , Double都有type inferred
28. Tuples 是多個值組合為一個複合值,tuple內可以任意的type。
ex:
let http404Error = (404, "Not Found")
這個tuple 型別是(Int String)
tuple可以是任意的type如(Int, Int,Int),也可以將一個tuple分解成單獨的常數和變數
let (statusCode, statusMessage) = http404Error
println("The status code is \(statusCode)" )
// output : The status code is 404
println("The status message is \(statusMessage)")
// output: The statusMessage is Not Found
29. 如果你只要tuple一部份的內容,你可以用 _ 來標記
ex:
let (justTheStatusCode, _ ) = http404Error
println("The status code is \(justTheStatusCode)")
30. 你也可以用索引值來取tuple的單個元素,索引從零開始
ex:
println("The status code is \(http404Error.0)")
println("The status message is \(http404Error.1)")
31. 你可以為tuple中的元素命名,透過名字來取得值
ex:
let http200Status = (statusCode:200, description:"OK")
取值
println("The status code is \(http200Status.statusCode)")
println("The status message is \(http200Status.description)")
tuple用來網路回傳值非常有用,一次可以包含兩種不同的type。
31. Optional 可以讓你表示任意型別的值不存在,不需要一個特殊的值。
ex:
String裡的toInt方法用來轉換型別至Int,但"hello world"無法轉成數字
let possibleNumber = "123"
let convertNumber = possibleNumber.toInt()
//回傳 Int?
Int? 代表是一個Optional Int,表示可能包含Int值也可能不包含值。
32. 你可以設置一個optiional變數為沒有值的狀,可以指派為nil.
ex:
var serverResponseCode:Int? = 404
serverResponseCode = nil
nil無法被使用在非optional的常數或變數,如果你的學數或變數在你的code可能會沒有值的
情況,通常宣告為optional值比較恰當。
如果你設置了optional的常數或變數沒有提供預設值 ,這個常數或變數會自動指派nil
ex:
var surveyAnswer:String?
// suveryAnswer會自動指派nil
33. swift的nil與objective-c 的nil不同,nil是指出不存在的物件,在swift nil不是pointer,是指不存的值或確切型態。任何型態的Optionals都可以設為nil,不是只有object型態。
34. 若確定optional有值的話,可以用 !來取值,代表確定這個optional有值做強制解析(forced unwrapping)
ex:
if convertedNumber != nil
{
println("convertedNumber has an integer value of \(convertedNumber!) .")
}
要確保 optional有值不為nil再使用 !,不然會產生runtime error
35. options bindings可以用if與while來對optional判斷,把值傳給一個常數或變數。
ex:
if let constantName = someOptional
{
statements
}
ex:
if let actualNumber = possibleNumber.toInt()
{
println("\(possibleNumber) has an integer value of \(actualNumber)")
}
else
{
println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an intger value of 123"
這是指如果Optional Int回傳optional Int包含一個值則創一個新的常數叫actualNumber,並將optional的值給他。actualNumber被用來輸出轉換結果,這裡不用再用!來抓取他的值。
你也可以改用if var actualNumber取代 ,存成變數。
// Implicitly Unwrapped Optionals
[未完 續待]
2. 可不用宣告明確的型別,var用來宣告變數,let用來宣告常數,
若要宣告明確的型別可以下寫法:
let explicitDouble:Double = 70
3. 變數無法直接做隱式轉換,若想要轉換變數到不同的型態,
需明確指定要轉什麼型態。
ex:
let label = "this is label"
let width = 99
let labelWidth = label + String(width)
4. 以上的寫法之外,可以用更簡易的方式來轉成字串
ex:
let apple = 3
let oranges = 5
let appleSum = "I have \(apple) apples."
let fruitSummary = "I have \(apple + oranges) pieces of fruit."
5. 建立array的方式與一般程式語言一樣,利用[] 與index來存取
ex:
var shoppingList =["fish", "water", "fruit"]
shoppingList[1] = "bottle of water"
ex:
var occupation = [
"Malcolm":"Captain",
"Kaylee":"Mechanic",
]
occupation["Jayne"] = "Public Relations"
6. 建立空的array與dictionary 利用以下初始化語法:
let emptyAry = [String]()
let emptyDictionary = [String: Float]()
7. 如果型態資訊能夠被推論,你可以寫一個空的array如[] 和空的dictionary如[:],
利如你設立一個新的值給變數,或傳一個參數到function
ex:
shppingList = []
occupations=[:]
宣告一個array變數:
ex:
var myStringAry:[String] = [“A","B","C","D"];
var
myStringAryOther:Array =["A","B","C","D"];
8. 與一般程式語言不同,if else 與 for-in , for, while, do-while不用加左右括號()
ex:
let individualScores=[9,7,5,3,1,2,4,6,8]
var teamScore = 0
for score in individualScores{
if score > 4{
teamScore+=3
}
else
{
teamScore+=1
}
}
teamScore
註:直接寫teamScore是在playground內直接看到內容值的方式
9. 在 if 敘述句內,這個敘對敘述句必需是Boolean描述,
像 if score {...}是錯誤的描述
10. swift整數分為8,16,32,64 位元的有號與無號整數型別,如
UInt8、Int32
11. 取得整數最小值與最大值範圍,可用 min與max屬性取得
ex:
let minValue = UInt8.min
let maxValue = UInt8.max
12. 通常你不需特別去選擇整數的size在你的程式碼,swfit提供Int,這個型態
的常度會自已對應與你現在的平台整數長度相同。即使在32Bit的平台下,Int
也能夠儲存 -2,147,483,648 ~ 2,147,483,647的範圍。
13. UInt 也另可分為UInt32、UInt64,長度也會依目前的平台對應。在swift非必要
盡量不要使用UInt,除非你真的需要目前平台的無號整數的長度。除了這個情況,
最好使用Int,即使你知道這是非負整數,統一使用Int可提高程式碼的重用性,避免
不同型別轉換,與匹配int type的推斷。
14. 浮點數範圍比Int更大。浮點數分為:Double、Float。
Double:代表64 bit浮點數。當你需要儲存很大或很高精度的浮點數。
Float:代表32 bit浮點數。精度要求不高可以使用此型別。
Double至少有15位數,而Float至少有6位數,選擇哪個型別取決於你的程式碼處理
範圍。
15. swift 是type safe的語言,會依你的變數型別去type checks。並且提供
type inference(型別推斷)。有了type inference就很少需要去宣告型別。
常數與變數雖需要明亮的型別,但大部份不需要由開發者自已完成。
例如
let meaningOfLife = 42。
這指meaningOfLife為Int型別
let pi = 3.14159
這指pi自已為Double型別。通常swift浮點數在推斷型別時,會選擇Double而非Float。
let anotherPi = 3 + 0.14159
此型別則為Double,因為3沒有明確指出是什麼型別。
16. 整數可以寫成
十進位:沒有前綴。
ex: let decimalInteger = 17
二進位:前綴是0b
ex: let binaryInteger = 0b1001 // 表示17
八進位:前綴是0o
ex: let octalInteger = 0o21 // 表示17
十六進位:前綴是0x
ex: let hexadecimalInteger = 0x11 // 表示17
17. 浮點字面可以是十進位或16進位(0x),小數點兩邊至少有一個十進位或十六進位。
浮點數有一個optional的指數(exponent),在十進位浮點數透過大寫或小寫的e來指定。
十六進位則透過p來指定。
ex:
1.25e2表示1.25x10^2等於125.0
1.25e-2表示
16進位指數表示:
ex:
0xFp2 表示 15x2^2 等於60.0
0xFp-2 表示 15x2^-2 等於3.75
18. 數值和浮點數可以添加額外的零或底線,不會影響數值。
ex:
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion=1_000_000.000_000_1
19. 通常即使知道整數變數或常數是已知的非負數,也請用Int型別。
20. 不同型別的整數可以儲存不同的範圍。
Int8:可儲存範圍 -128~127
UInt8: 可儲存 0~255
ex:
let cannotBeNegative: UInt8 = -1
// 會報error 不能存負數
let tooBig:Int8 = Int8.max +1
// 超過最大數值會報錯
21. 每個整數可儲存不同範圍值,必須在程式依不同情況選擇數值型型別轉換,
可預防隱式轉換的錯誤。
22. 以下例子twoThousand是 UInt16 type,而one是 UInt8 type,兩者不能相加,
需將one轉換成UInt16才可相加。
ex:
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
twoThousandAndOne則被推斷為UInt16的type,因為兩個UInt16 type值相加。
23. SomeType(ofInitialValue)是呼叫UInt16一個初始化的函式,可接受UInt8的型別值。
這個初始化可以讓現有的UInt8創建一個新的UInt16 type。
24. 整數與浮點數必須做顯示指定型別轉換。
ex:
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
25. 浮點數轉整數。
ex:
let integerPi = Int(pi)
26. 可以利用typealias來定義型別別名
ex:
typealias AudioSample = UInt16
var maxAmplitudeFoud = AudioSample.min
// maxAmplitudeFoud 現在為0
AudioSample就是UInt16
27. Swift的布林型態為Bool ,供兩種值分別為 true , false。與Int , Double都有type inferred
28. Tuples 是多個值組合為一個複合值,tuple內可以任意的type。
ex:
let http404Error = (404, "Not Found")
這個tuple 型別是(Int String)
tuple可以是任意的type如(Int, Int,Int),也可以將一個tuple分解成單獨的常數和變數
let (statusCode, statusMessage) = http404Error
println("The status code is \(statusCode)" )
// output : The status code is 404
println("The status message is \(statusMessage)")
// output: The statusMessage is Not Found
29. 如果你只要tuple一部份的內容,你可以用 _ 來標記
ex:
let (justTheStatusCode, _ ) = http404Error
println("The status code is \(justTheStatusCode)")
30. 你也可以用索引值來取tuple的單個元素,索引從零開始
ex:
println("The status code is \(http404Error.0)")
println("The status message is \(http404Error.1)")
31. 你可以為tuple中的元素命名,透過名字來取得值
ex:
let http200Status = (statusCode:200, description:"OK")
取值
println("The status code is \(http200Status.statusCode)")
println("The status message is \(http200Status.description)")
tuple用來網路回傳值非常有用,一次可以包含兩種不同的type。
31. Optional 可以讓你表示任意型別的值不存在,不需要一個特殊的值。
ex:
String裡的toInt方法用來轉換型別至Int,但"hello world"無法轉成數字
let possibleNumber = "123"
let convertNumber = possibleNumber.toInt()
//回傳 Int?
Int? 代表是一個Optional Int,表示可能包含Int值也可能不包含值。
32. 你可以設置一個optiional變數為沒有值的狀,可以指派為nil.
ex:
var serverResponseCode:Int? = 404
serverResponseCode = nil
nil無法被使用在非optional的常數或變數,如果你的學數或變數在你的code可能會沒有值的
情況,通常宣告為optional值比較恰當。
如果你設置了optional的常數或變數沒有提供預設值 ,這個常數或變數會自動指派nil
ex:
var surveyAnswer:String?
// suveryAnswer會自動指派nil
33. swift的nil與objective-c 的nil不同,nil是指出不存在的物件,在swift nil不是pointer,是指不存的值或確切型態。任何型態的Optionals都可以設為nil,不是只有object型態。
34. 若確定optional有值的話,可以用 !來取值,代表確定這個optional有值做強制解析(forced unwrapping)
ex:
if convertedNumber != nil
{
println("convertedNumber has an integer value of \(convertedNumber!) .")
}
要確保 optional有值不為nil再使用 !,不然會產生runtime error
35. options bindings可以用if與while來對optional判斷,把值傳給一個常數或變數。
ex:
if let constantName = someOptional
{
statements
}
ex:
if let actualNumber = possibleNumber.toInt()
{
println("\(possibleNumber) has an integer value of \(actualNumber)")
}
else
{
println("\(possibleNumber) could not be converted to an integer")
}
// prints "123 has an intger value of 123"
這是指如果Optional Int回傳optional Int包含一個值則創一個新的常數叫actualNumber,並將optional的值給他。actualNumber被用來輸出轉換結果,這裡不用再用!來抓取他的值。
你也可以改用if var actualNumber取代 ,存成變數。
// Implicitly Unwrapped Optionals
[未完 續待]
2014年9月15日 星期一
Android 分享文字訊息至Line
Android 指定開Line App傳入文字訊息
通常Android要開啟第三方App需知道對方app的package name
上面的方法 可以抓出手機內所有的App package name
而你會發現Line的App Package Name的名稱是 jp.naver.line.android
故當程式可用此方式來檢查是否裝此app
若要從程式碼傳送文字至 Line訊息的話
上面是送文字訊息
而AndroidManifest.xml 記得要加接收到ACTION_SEND的intent filter
通常Android要開啟第三方App需知道對方app的package name
PackageManager pm = mContext.getPackageManager();
List<applicationinfo> appList = pm.getInstalledApplications(0);
for(ApplicationInfo app: appList )
{
Log.i("info","app:" + app );
if( app.packageName.equals(LINE_PACKAGE_NAME))
{
return true;
}
}
上面的方法 可以抓出手機內所有的App package name
而你會發現Line的App Package Name的名稱是 jp.naver.line.android
故當程式可用此方式來檢查是否裝此app
若要從程式碼傳送文字至 Line訊息的話
intent.setAction(Intent.ACTION_SEND);
intent = mContext.getPackageManager().getLaunchIntentForPackage(AppConfig.LINE_PACKAGE_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is test! oh ya!");
mContext.startActivity(intent);
上面是送文字訊息
而AndroidManifest.xml 記得要加接收到ACTION_SEND的intent filter
<intent-filter >
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter >
訂閱:
文章 (Atom)



