顯示具有 比較 標籤的文章。 顯示所有文章
顯示具有 比較 標籤的文章。 顯示所有文章

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月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年9月4日 星期四

BOOL vs. Boolean vs. bool

Boolean is an old Carbon keyword (historic Mac type), defined as an unsigned char.
BOOL is an Objective-C type defined as signed char.
bool is a defined version of the _Bool standard C type.

You should use BOOL for common use and Boolean in CF.

資料來源:http://stackoverflow.com/a/3016912/3295047