2014年10月30日 星期四

int vs. NSInteger vs. NSNumber

NSInteger vs. int

NSInteger is typedef'ed based on the target architecture. In other word, it's an architecture-safe (64 vs 32 bit) type to support different platforms and implementations of C. Targeting a 32-bit CPU and OS it's 32-bits wide, on a 64-bit OS it's 64-bits wide.

What follows is how NSInteger is defined in NSObjCRuntime.h file :

And this is what Apple say in Foundation Data Types Reference :
When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer.

Simply, the NSInteger typedef does a step for you: if the architecture is 32-bit, it uses int, if it is 64-bit, it uses long. Using NSInteger, you don't need to worry about the architecture that the program is running on. 

Apple recommends that you use NSInteger over normal types anyway, I would assume for portability!

結論:除非你有特定要int或是long,要不然就建議用NSInteger,這樣就不必去煩惱使用者用的是32-bit還是64-bit的iDevice。

更深入探討

Why use int at all?
Apple uses int because for a loop control variable (which is only used to control the loop iterations) int datatype is fine, both in datatype size and in the values it can hold for your loop. No need for platform dependent datatype here. For a loop control variable even a 16-bit int will do most of the time.
Apple uses NSInteger for a function return value or for a function argument because in this case datatype [size] matters, because what you are doing with a function is communicating/passing data with other programs or with other pieces of code; see the answer to When should I be using NSInteger vs int?in your question itself...
Apple use NSInteger (or NSUInteger) when passing a value as an argument to a function or returning a value from a function.
資料來源:http://stackoverflow.com/a/5320359/3295047

延伸閱讀:64-Bit Transition Guide for Cocoa Touch



NSNumber

NSNumber is an Objective-C class, a subclass of NSValue to be specific. Where a NSInteger or int will fit in a register, a NSNumber is an object that can hold/encapsulate any scalar type (IIRC - I don't use NSNumber if I can help it.  Way too slow.), It can be used when you need an NSObject that holds a scalar value.

You can create an NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL.

One of the primary distinctions is that you can use NSNumber in collections, such as NSArray or NSSet, where an object is required. For example, if you need to add a float into an NSArray, you would first need to create an NSNumber object from the float:
1
2
3
4
float percentage = 40.5;
...
// Create NSNumber object, which can now be inserted into an NSArray
NSNumber *percentageObject = [NSNumber numberWithFloat:percentage];

Cocoa提供了NSNumber類來包裝(即以物件object形式實現)基本數據類型。

例如以下創建方法:
+ (NSNumber*)numberWithChar: (char)value;
+ (NSNumber*)numberWithInt: (int)value;
+ (NSNumber*)numberWithFloat: (float)value;
+ (NSNumber*)numberWithBool: (BOOL) value;
例:NSNumber *n = [NSNumber numberWithInt:5];

將基本類型數據封裝到NSNumber中後,就可以通過下面的實例方法重新獲取它:
- (char)charValue;
- (int)intValue;
- (float)floatValue;
- (BOOL)boolValue;
- (NSString*)stringValue;
例:int i = [n intValue];



資料來源:
http://www.quora.com/What-is-the-difference-between-NSInteger-NSNumber-int-and-all-the-different-ways-to-represent-an-integer
http://iosdevelopertips.com/cocoa/nsnumber-and-nsinteger.html
http://stackoverflow.com/a/5870916/3295047
http://stackoverflow.com/a/4445224/3295047
http://www.wuleilei.com/blog/335

2014年10月26日 星期日

Local Notification介紹

基本設定

請求使用者運允許發送通知
從iOS 8開始,如果打完上面的程式就執行會出現類似下面這樣的錯誤碼

要把下面這段程式加上去,請求使用者允許App發送通知,我們的Local Notification才能正常運作,在iOS 8之前當然也要使用者的同意,不過可能以前是系統自動幫我們問吧,不過就像我常講的,其實我們不太需要知道以前是怎麼樣,反正現在這段程式是一定要寫就對了!

自訂特定的時間
一般fireDate的設定我們都是使用我們既有的資料或是稍微經過dateByAddingTimeInterval之類的處理,如果要完全自訂一個特定的時間的話,可以參考下面的程式碼。你會注意要我只設定了小時跟分鐘,如果想要設定通知是每天固定發出,只要設定小時跟分鐘就可以了。

alertAction
這段程式有個小問題,它可以執行,但我必須要自己去「設定」那裏把alert sytle從Banners改成Alerts,notification才會顯示成alert的形式,我目前還不知道問題出在哪裡,知道的人拜託告訴我~

icon Badge
注意第三行程式,我們也可以讀取目前的IconBadgeNumber做累加的動作

重複

使用自訂的聲音
我沒想到要使用自訂的聲音竟然這麼簡單,只要將想要播放的音檔匯入project中,再如下面的程式碼打就可以了,不過有兩點需要注意:
1音檔的長度不可以超過三十秒,要不然聲音會播不出來,我在Documentation裡沒看到這項規定,是在stackoverflow裡看到這篇回答才知道的,但我確定有這項限制,因為我之前就是卡關在這裡Orz
2音檔只有一些特定的格式可以

在通知中另外夾帶參數(一般是供其它地方的程式辨識用)

取消Local Notification
取消指定的通知
其實沒有一定要用下面的方式來尋找指定的Local Notification,也可以用其他屬性像是fireDate、alertBody...等,只要你能找到你要的特定通知就可以了。另外,雖然可以抓到自己想要的特定通知,但我試過並不能再修改其屬性。

取消全部

按下發送出來的通知後怎麼處理
當App已經沒有在運作

當App已經還在運作(可能在背景或是或是正在使用)


更深入的探討
https://www.facebook.com/groups/iostw/permalink/906779296016087/
https://www.facebook.com/groups/iostw/permalink/934380229922660/
https://www.facebook.com/photo.php?fbid=10202759629864986&set=gm.967383696622313&type=1

用Local Notification實現Alarm
要實作一個Alarm需要做到下面這幾點:
1播放音樂
2在背景執行
3定時
其實上面三點是一句話拆開來講:「在一段時間後在背景播放音樂」
我查了很多資料,發現目前Local Notification是唯一可以完全達到上面要求的方法,但那只是從技術面來看,實際使用的話會遇到兩個問題:
1.1Local Notification的聲音最長只能到三十秒,如果要做的一個鬧鐘的程式,我們絕對不可能讓他只響三十秒,當然我們可以一連排程好幾的Local Notification,可是這樣使用者打開他的iPhone的時候就是看到四五個Notification,這樣的使用者體驗非常差。
1.2我目前想到比較好的方法是:設一連串的Local Notification,只有第一個有設alertBody,後面幾個都設nil,因為我發現如果alertBody設nil,iDevice不會顯示任何東西,但音樂還是會播放。
1.3不過後來經過測試,這樣的UX爛炸了,因為一般我要用自訂的聲音,音檔一般都會像是一首歌之類的,用上面的方法,這首歌每次只會播三十秒就重新播放,越聽只讓我越煩燥而已。
2沒辦法像內建的鬧鐘可以有按鈕讓鬧鐘稍後再播放

我去網路上問,不過看起來應該是無解了Orz

2014年10月25日 星期六

Xcode相關工具 (未整理)

  1. Faux Pas for Xcode:可以幫你找出程式碼中各種隱藏起來的臭蟲(三十天試用版、個人版89鎂、企業版189鎂)
  2. RunEverywhere – An Xcode plugin allowing you to run and stop projects on multiple physical iOS devices.
  3. Remote – An Xcode plugin for automatic on-device testing allowing you to record and playback macros that run on device.
  4. RTImageAssets – A configurable Xcode plugin that generates missing image assets based on missing sizes.
  5. rest2Mobile – An Xcode plugin that allows you to automatically generate code for a REST service.
  6. DBSmartPanels – A configurable plugin allowing you to automatically hide the debugger and utilities panels when unneeded.
  7. MarvinXcode – A plugin adding many hotkeyed ccommands for easier selection, duplication, and deletion of code.
  8. MCLog – A plugin that enhances the debug console with output filtering that can work in real time and supports regular expressions.
  9. GitDiff – A plugin for easy visualization of modified and changed code based on the projects Git repo.
  10. ACCodeSnippetRepositoryPlugin – A plugin allowing you to seamlessly integrate the Xcode snippets library with a Git repository.
  11. Extractor Localizable Strings – A plugin allowing you to quickly turn a string into a localizable string bringing up a popup where you can specify the localized string’s key on hotkey press.
  12. Peckham – A plugin allowing you to bring up a popup for quickly creating import statements from anywhere within your code.
  13. XCFui – A plugin allowing you to easily identify unused imports within your code bringing them up as warnings on compilation.
  14. XprobePlugin – A plugin allowing you to easily browse your application’s memory  in a nice UIWebView based interface with filtering and searching capabilities.
  15. XcodeBoost – A plugin adding many nice features such as allowing you to extract method declarations from your implementation to paste in the header file, highlight based on regex, paste lines while preserving code formatting and more..
  16. WCGitTagsPlugin – A plugin allowing you to easily add, view, and remove Git tags through a GUI within Xcode.
  17. BBUFullIssueNavigator – A plugin allowing you to display full content in the Xcode issue navigator (no more ellipsis).
  18. BBUDebuggerTuckAway – A plugin that automatically hides the debugger when you start typing within the code editor.
  19. CodePilot – Allows you to quickly search through Xcode projects using keywords allowing you to quickly search and navigate through an Xcode project. Formerly paid plugin now open source and working with Xcode 5.
  20. ClangFormat-Xcode – A plugin that allows you to format code using the Clang Format tool allowing you to chose from a number of preset configurations or your own.
  21. XToDo – A plugin that provides an interface allowing you to jump through todo’s, within your code labeled by your comments.
  22. XCAddedMarkup – A plugin that allows you enabling display of images and hyperlinks within your code using a special markup syntax.
  23. XAlign – A plugin for aligning your code automatically in user definable ways with a number of sample alignments such as align to first euqals included.
  24. ShowInGithub – An Xcode plugin allowing you to quickly jump to the corresponding location for a line or block of code on Github or Bitbucket.
  25. KFCocoaPods – A plugin for working with Cocoapods providing easy downloading and updating of CocoaPods, podfile editing with code completion, and CocoaPods output in the console.  (Xcode 5 tested)
  26. SCXCodeMiniMap – Creates a minimap of your code adjusting the color of the currently visible area of the editor so you can quickly see where you are within your code.
  27. Injection – Allows you to inject code in real-time to an app running in the simulator or device  so that you can tweak your code without recompiling.  Formerly a paid plugin, but now available as open source.
  28. Xcode Fixins - A collection of plugins for minimizing distractions in Xcode, and fixing code completion.
  29. XVim – Adds many Vim key bindings to Xcode.
  30. Mini-Xcode – Allows you to remove the large Xcode IDE toolbar to save screen space providing mini scheme and device selection.
  31. ColorSense – Provides a color picker for adjustments when you highlight a UIColor or NSColor within your code.
  32. BBUncrustifyPlugin -Enables easy use of the Uncrustify code beautification tool.
  33. Lin – Provides a neat interface directly within Xcode to make working with NSLocalizedString easier.
  34. KSImageNamed – Provides auto completion for UIImage imageNamed: within your code by scanning through image files within your workspace.
  35. HOStringSense – Provides a call out bubble in which you can work with an unescaped version of a string.
  36. XcodeColors – Allows you to color code messages in the debugging console so you can make errors stand out and easily separate different messages from differing parts of your code.
  37. AutoresizeMask – Allows you to visualize your autoresizing masks within code.
  38. JDListInstalledPlugins – Allows you to list your installed plugins, and one click remove.
  39. Alcatraz – Provides easy install/removal of Xcode plugins, color schemes, and templates.  A newer project so there is not too much in there, but a good idea.
RunEverywhere – An Xcode plugin allowing you to run and stop projects on multiple physical iOS devices.
Remote – An Xcode plugin for automatic on-device testing allowing you to record and playback macros that run on device.
RTImageAssets – A configurable Xcode plugin that generates missing image assets based on missing sizes.
rest2Mobile – An Xcode plugin that allows you to automatically generate code for a REST service.
DBSmartPanels – A configurable plugin allowing you to automatically hide the debugger and utilities panels when unneeded.
MarvinXcode – A plugin adding many hotkeyed ccommands for easier selection, duplication, and deletion of code.
MCLog – A plugin that enhances the debug console with output filtering that can work in real time and supports regular expressions.
GitDiff – A plugin for easy visualization of modified and changed code based on the projects Git repo.
ACCodeSnippetRepositoryPlugin – A plugin allowing you to seamlessly integrate the Xcode snippets library with a Git repository.
Extractor Localizable Strings – A plugin allowing you to quickly turn a string into a localizable string bringing up a popup where you can specify the localized string’s key on hotkey press.
Peckham – A plugin allowing you to bring up a popup for quickly creating import statements from anywhere within your code.
XCFui – A plugin allowing you to easily identify unused imports within your code bringing them up as warnings on compilation.
XprobePlugin – A plugin allowing you to easily browse your application’s memory  in a nice UIWebView based interface with filtering and searching capabilities.
XcodeBoost – A plugin adding many nice features such as allowing you to extract method declarations from your implementation to paste in the header file, highlight based on regex, paste lines while preserving code formatting and more..
WCGitTagsPlugin – A plugin allowing you to easily add, view, and remove Git tags through a GUI within Xcode.
BBUFullIssueNavigator – A plugin allowing you to display full content in the Xcode issue navigator (no more ellipsis).
BBUDebuggerTuckAway – A plugin that automatically hides the debugger when you start typing within the code editor.
CodePilot – Allows you to quickly search through Xcode projects using keywords allowing you to quickly search and navigate through an Xcode project. Formerly paid plugin now open source and working with Xcode 5.
ClangFormat-Xcode – A plugin that allows you to format code using the Clang Format tool allowing you to chose from a number of preset configurations or your own.
XToDo – A plugin that provides an interface allowing you to jump through todo’s, within your code labeled by your comments.
XCAddedMarkup – A plugin that allows you enabling display of images and hyperlinks within your code using a special markup syntax.
XAlign – A plugin for aligning your code automatically in user definable ways with a number of sample alignments such as align to first euqals included.
ShowInGithub – An Xcode plugin allowing you to quickly jump to the corresponding location for a line or block of code on Github or Bitbucket.
KFCocoaPods – A plugin for working with Cocoapods providing easy downloading and updating of CocoaPods, podfile editing with code completion, and CocoaPods output in the console.  (Xcode 5 tested)
SCXCodeMiniMap – Creates a minimap of your code adjusting the color of the currently visible area of the editor so you can quickly see where you are within your code.
Injection – Allows you to inject code in real-time to an app running in the simulator or device  so that you can tweak your code without recompiling.  Formerly a paid plugin, but now available as open source.
Xcode Fixins - A collection of plugins for minimizing distractions in Xcode, and fixing code completion.
XVim – Adds many Vim key bindings to Xcode.
Mini-Xcode – Allows you to remove the large Xcode IDE toolbar to save screen space providing mini scheme and device selection.
ColorSense – Provides a color picker for adjustments when you highlight a UIColor or NSColor within your code.
BBUncrustifyPlugin -Enables easy use of the Uncrustify code beautification tool.
Lin – Provides a neat interface directly within Xcode to make working with NSLocalizedString easier.
KSImageNamed – Provides auto completion for UIImage imageNamed: within your code by scanning through image files within your workspace.
HOStringSense – Provides a call out bubble in which you can work with an unescaped version of a string.
XcodeColors – Allows you to color code messages in the debugging console so you can make errors stand out and easily separate different messages from differing parts of your code.
AutoresizeMask – Allows you to visualize your autoresizing masks within code.
JDListInstalledPlugins – Allows you to list your installed plugins, and one click remove.
Alcatraz – Provides easy install/removal of Xcode plugins, color schemes, and templates.  A newer project so there is not too much in there, but a good idea.
資料來源:https://maniacdev.com/xcode-plugins


Xcode Plugin Listing – Quality Xcode Plugins
Alcatraz:The package manager for Xcode

CodePilot

2014年10月16日 星期四

atomic vs. nonatomic

nonatomic :
  • does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program.


atomic(default) : 我目前有找到幾種解釋,因為還沒整理完,所以先都貼上來。
  • enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program
  • More precisely, tomic properties do not ensure thread-safety; rather it ensures atomicity. If thread A and thread B are both writing, atomic ensures that the outcome will be a whole value, meaning either one or the other. Which one is undefined. Writing thread-safe code is not as simple as using atomic properties. See the "Synchronization" section of the Threading Programming Guide
  • atomic是Objc使用的一種線程保護技術,基本上來講,是防止在寫未完成的時候被另外一個線程讀取,造成數據錯誤。而這種機制是耗費系統資源的,所以在iPhone這種小型設備上,如果沒有使用多線程間的通訊編程,那麼 nonatomic 是一個非常好的選擇。
  • Atomicity has to do with how properties behave in a threaded environment. When you have more than one thread, it’s possible for the setter and the getter to be called at the same time. This means that the getter/setter can be interrupted by another operation, possibly resulting in corrupted data. Atomic properties lock the underlying object to prevent this from happening, guaranteeing that the get or set operation is working with a complete value. However, it’s important to understand that this is only one aspect of thread-safety—using atomic properties does not necessarily mean that your code is thread-safe.

整理來源:
http://stackoverflow.com/a/10753201/3295047
http://stackoverflow.com/questions/9162926/ios-property-declaration-clarification
http://rypress.com/tutorials/objective-c/properties.html

2014年10月13日 星期一

吳祥輝書單

1. 「拒絕聯考的小子」1975
2. 「斷指少年」 1977
3. 「拒絕國民黨的小子」 1984
4 「李敖死了」 1986
5 「吳祥輝選舉學」 2002
4. 「芬蘭驚豔」 2006
5. 「驚歎愛爾蘭」 2007
6. 「我是被老師教壞的」 2008
7. 「驚喜挪威」 2009
8. 「陪你走中國」 2010
9 「驚恐日本」 2011


吳祥輝:「芬蘭驚豔 驚歎愛爾蘭 驚喜挪威
是想讓台灣人更了解自己
因為我從沒認識過一個真正認識台灣的人
您可別把書看錯了
這其實不是獨立的三本書
而是一本書的上中下集
陪你走中國 驚恐日本
是想讓台灣人更了解別人
因為我也從沒認識過一個了解日本和中國的台灣人」
原網址:https://www.facebook.com/brianwuhsianghui/posts/723469104401713

吳祥輝:「驚恐日本是繼芬蘭驚豔 驚歎愛爾蘭 驚喜挪威
陪你走中國 後的 第五本 國家書寫
也是父子三部曲的第二部
父子三部曲是
陪你走中國 驚恐日本 和寫作中的韓國
台灣人可以用全新的角度
看中國 日本 南韓 這三個鄰國
這是寫這三個國家的本意」
原網址:https://www.facebook.com/brianwuhsianghui/posts/723465251068765