ITEM3 :优先使用字面值而非与其对等的方法。
- 创建NSString,NSNumber,NSArray,NSDictionary以及他们的可变类,尽量采用字面值语法的形式去创建,这样可以减少代码冗余,看起来更简洁。
- 访问数组或者字典的某一个值时,采用下标的形式来获取值。
- 需要注意的是采用字面值去创建数组或者字典的时候不要插入nil,否则会抛出异常。
代码示例:
Literal NSString
Objective-C
1
2
3
|
NSString
*str
=
[
[
NSString
alloc
]
initWithFormat
:
@"%@"
,
@"Hello World"
]
;
---->字面值如下
NSString
*str
=
@"Hello World"
;
|
Literal Numbers
Objective-C
1
2
3
4
5
6
7
|
NSNumber
*someNumber
=
[
NSNumber
numberWithInt
:
1
]
;
---->字面值如下
NSNumber
*intNumber
=
@
1
;
NSNumber
*floatNumber
=
@
2.5f
;
NSNumber
*doubleNumber
=
@
3.14159
;
NSNumber
*boolNumber
=
@
YES
;
NSNumber
*charNumber
=
@
'a'
;
|
Literal Arrays
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
NSArray
*animals
=
[
NSArray
arrayWithObjects
:
@"cat"
,
@"dog"
,
@"mouse"
,
@"badger"
,
nil
]
;
---->字面值如下
NSArray
*animals
=
@
[
@"cat"
,
@"dog"
,
@"mouse"
,
@"badger"
]
;
NSString
*dog
=
[
animals
objectAtIndex
:
1
]
;
---->通过字面值取数据
NSString
*dog
=
animals
[
1
]
;
//可变的话可以直接设置新值
[
mutableArray
replaceObjectAtIndex
:
1
withObject
:
@"dog"
]
;
---->字面值设置值
mutableArray
[
1
]
=
@"dog"
;
NSString
*str1
;
NSString
*str2
=
@"1"
;
NSString
*str3
=
@"1"
;
///插入空数据会抛出异常
NSArray
*arr
=
@
[
str1
,
str2
,
str3
]
;
//reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
//这种方式不会抛异常,但是数组会收到第一个nil就终止了。所以采用字面值形式会得到错误提示。
NSArray
*arr
=
[
NSArray
arrayWithObjects
:str1
,
str2
,
str3
,
nil
]
;
NSLog
(
@"arr.count=%d"
,
arr
.
count
)
;
//0
NSLog
(
@"arr.first=%@"
,
arr
.
firstObject
)
;
//null
|
Literal Dictionaries
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
NSDictionary
*personData
=
[
NSDictionary
dictionaryWithObjectsAndKeys
:
@"Kevin"
,
@"firstName"
,
@"Jin"
,
@"lastName"
,
[
NSNumber
numberWithInt
:
25
]
,
@"age"
,
nil
]
;
---->字面值如下
NSDictionary
*personData
=
@
{
@"firstName"
:
@"Kevin"
,
@"lastName"
:
@"Jin"
,
@"age"
:
@
25
}
;
NSString
*str1
;
NSString
*str2
=
@"1"
;
NSString
*str3
=
@"1"
;
//和数组一样,value不能有nil ,否则就会抛出异常。
NSDictionary
*dic
=
@
{
@"str1"
:str1
,
@"str2"
:str2
,
@"str3"
:str3
}
;
//reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
//不会提示你错误
NSDictionary
*dic
=
[
NSDictionary
dictionaryWithObjectsAndKeys
:str1
,
@"str2"
,
str2
,
@"str2"
,
str3
,
@"str3"
,
nil
]
;
NSLog
(
@"dic.count=%d"
,
dic
.
count
)
;
//0
//取值
NSString
*lastName
=
[
personData
objectForKey
:
@"lastName"
]
;
---->字面值取值
NSString
*lastName
=
personData
[
@"lastName"
]
;
///可变的话可以这样设置新值
[
mutableDictionary
setObject
:
@"Galloway"
forKey
:
@"lastName"
]
;
---->字面值设置值
mutableDictionary
[
@"lastName"
]
=
@"Galloway"
;
|
ITEM4:优先使用带类型的常量而不是直接使用#define来定义常量
- 定义一个动画执行时间大都会#define ANIMATION_DURATION 0.3,但是这个宏给出的信息量不够,大概能猜出是个时间值,如果采用static const NSTimeInterval kAnimationDuration = 0.3 这种形式,那么一看就知道这是个时间值。
- #define出来在别处重定义不会给出任何提示,如果使用const修饰,当你试图去修改其值时会提示你。
- 常量命名,一般采用前缀大写K的形式。如kAnimationDuration;如果这个常量要暴露给外部使用通常前缀为类名。
- 如果定义的常量对外部可用,使用extern修饰词,在.h中声明,.m中定义。前缀采用类名。常用于通知名字,比如系统的某些通UIApplicationDidEnterBackgroundNotification ,UIApplicationWillEnterForegroundNotification.都是这种形式。如果常量对外不可用,则直接在.m中定义,前缀大写K。
代码示例:
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// EOCAnimatedView.h
#import <UIKit/UIKit.h>
@interface
EOCAnimatedView
: UIView
-
(
void
)
animate
;
@end
// EOCAnimatedView.m
#import "EOCAnimatedView.h"
static
const
NSTimeInterval
kAnimationDuration
=
0.3
;
///常量,前缀大写K
@implementation
EOCAnimatedView
-
(
void
)
animate
{
[
UIView
animateWithDuration
:kAnimationDuration
animations
:
^
(
)
{
///
///
}
]
;
}
@end
|
Objective-C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// EOCLoginManager.h
#import <Foundation/Foundation.h>
extern
NSString
*const
EOCLoginManagerDidLoginNotification
;
//常量,外部可用,此处声明。类名命名法。
@interface
EOCLoginManager
: NSObject
-
(
void
)
login
;
@end
// EOCLoginManager.m
#import "EOCLoginManager.h"
NSString
*const
EOCLoginManagerDidLoginNotification
=
@"EOCLoginManagerDidLoginNotification"
;
//常量实现处。
@implementation
EOCLoginManager
-
(
void
)
login
{
// Perform login asynchronously, then call 'p_didLogin'.
}
-
(
void
)
p
_
didLogin
{
[
[
NSNotificationCenter
defaultCenter
]
postNotificationName
:EOCLoginManagerDidLoginNotification
object
:nil
]
;
}
@end
|
Objective-C
1
2
3
4
5
6
|
// EOCAnimatedView.h
extern
const
NSTimeInterval
EOCAnimatedViewAnimationDuration
;
// EOCAnimatedView.m
const
NSTimeInterval
EOCAnimatedViewAnimationDuration
=
0.3
;
|