This is the 22nd day of my participation in the August More Text Challenge
Use prefixes to avoid naming conflicts
-
Prefixes the name of a company, application, or association that is related to both
-
The usual prefix should be three letters (Apple keeps the two-letter prefix).
-
Always prefix the classification
-
Write tripartite library, must add prefix, use other tripartite library, should add your prefix in front of other tripartite library, avoid others integration into the conflict with other tripartite library
For example: your third party library uses YYLibrary library, your project library is called QQLibrary, after modification should be QQYYLibrary
Clear and coordinated naming
-
Name a method for considerations
- If the method return value is newly created, the first word of the method name should be the type of the return value, unless preceded by a modifier
- You should place the parameter type noun before the parameter
- If a method performs an operation on the current object, it should include a verb; If you need parameters to perform an operation, you should follow the verb with one or more nouns
- Don’t use short names like STR, use full names like string
BOOL
The value property should be addedis
Prefix if a method returns a value that is not its propertyBOOL
Value, then should be processed according to its function, selectionhas
oris
When the prefix- will
get
This prefix is reserved for methods that store the return value by “output parameters.
-
Naming of classes and protocols
Prefix the names of classes and protocols
-
Private methods are prefixed
- Private methods are prefixed to facilitate debugging and to distinguish them from public apis
- Do not prefix private methods with an underscore; the underscore is reserved by Apple
Use immutable objects whenever possible
- When using an attribute, you can declare it as”
readonly
“(Default:readwrite
) - For example, if a property is passed in through an initialization method, the value of a property that is declared separately can be declared as
readonly
#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
@end
Copy the code
- If you want to modify data without letting outsiders know, you can usually set it to
readwrite
#import "Phone.h"
// redeclare the classification
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@end
@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
self = [super init];
if (self) {
_name = [name copy];
_price = price;
}
return self;
}
@end
Copy the code
- Try not to put variable
array
,set
,dictionary
As public attributes, you should provide methods to modify mutable attributes in an object
Why can’t you just define phoneArray as mutable and control it with phoneArray? For example, Phone objects may need to perform other operations when adding or deleting phoneArray. If you modify the internal phoneArray for storage directly from the bottom layer, data inconsistency may occur among Phone objects if you modify phoneArray without Phone objects’ knowledge
#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
@property (nonatomic,strong,readonly) NSArray *phoneArray;
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
- (void)addPhone:(Phone *)phone;
- (void)removePhone:(Phone *)phone;
@end
#import "Phone.h"
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@property (nonatomic,strong,readwrite) NSMutableArray *savePhoneArray;
@end
@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
self = [super init];
if (self) {
_name = [name copy];
_price = price;
_savePhoneArray = [[NSMutableArray alloc]init];
}
return self;
}
- (instancetype)init{
return [self initWithName:@"iPhone" withPrice:999];
}
-(NSMutableArray *)savePhoneArray{
if (_savePhoneArray == nil) {
_savePhoneArray = [NSMutableArray new];
}
return _savePhoneArray;
}
- (void)addPhone:(Phone *)phone{
[self.savePhoneArray addObject:phone];
}
- (NSArray *)phoneArray{
return [self.savePhoneArray copy];
}
- (void)removePhone:(Phone *)phone{
[self.savePhoneArray removeObject:phone];
}
@end
Copy the code
Knowledge about iOS interface and API design
- Interface and API Design in iOS (part 1)
- Interface and API Design in iOS (II)
- Interface and API Design in iOS (iii)