In arc, properties outside the basic variables are preferred for weak references.weakTo avoid the appearance of wild pointers,weakCan be automatically set to when the pointed object is deallocnil(The attribute value will be cleared).
Used in arcassignModificationdelegateIs very dangerous, inassignProperty values are not cleared when the decorated property is destroyed. If you use this timeself.delegateWill cause a programcrash。
Strong reference in arc to try to usestrongOf course herestrongwithretainThe role is the same, but in order to maintain code consistency, it is recommended to use here.strong。
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
There are two access methods: 1. Access by property, ie setter getter method. 2. Direct access method.
Access to properties when setting instance variables, ieself. name. Because of direct access to instance variables_name, does not call the setter method, it also bypasses the 'memory management semantics' defined for the property (ie the modifiers copy, strong, weak... does not work). Direct access also does not trigger the 'key-value observation KVO' notification. If the property uses lazy loading, use direct access_nameIt is also invalid.
NSString *const kNotificationName = @"failNotificationName Name";
If the constant is not global, add static before it. Otherwise a duplicate symbol error may occur.
If you need to declare a global constant, you need to include it in the .h file.
FOUNDATION_EXPORT NSString * const kNotificationName;
or
extern NSString * const kNotificationName;
Similarly we can use macros to declare variables and define preprocessor directives. There are several disadvantages to using preprocessing directives: 1. The defined constants have no type information and can only be replaced. 2. When the macro is modified elsewhere, it will cause the constant to change. (For example, other developers define macros with the same name in the project, the compiler will not report an error) and using type constants can solve these two problems.
In the development process, it is often necessary to switch environments. Such as test environment, development environment, and production environment. We can return the url by constructing a category of NSString.
#import "NSString+URL.h"
static NSString * const kBaseURL = @"http://220.175.104.19:8080";
@implementation NSString (URL)
+ (NSString *)RequestUrlWithString:(NSString *)url
{
return [NSString stringWithFormat:@"%@%@",kBaseURL, url];
}
@end
Instructions:
NSString *url = [NSString RequestUrlWithString:@"xxx/xxxx"];
See a colleague using this method to add notifications and remove notifications
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil] ;
[[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil] ;
}
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter ] removeObserver:self name:UIKeyboardWillHideNotification object:nil ] ;
[[NSNotificationCenter defaultCenter ] removeObserver:self name:UIKeyboardWillShowNotification object:nil ];
}
becausewillAppearwithDisappearThe order of appearance is not necessarily one-to-one. So it is possible to cause multiple additions and remove notifications multiple times. The view controller cannot receive notifications when the view is not showing, causing some operations not to proceed.
is recommended inviewDidLoadAdd a notification. indeallocRemove notifications. Of course, this method can be used when notifying the root view controller to display the operation, avoiding all the loaded controllers responding to the notification.
inviewDidLoadAfter that, the initialization process must generate the corresponding control or data, regardless of whether these controls or data are immediately useful, which will take up a relatively large memory space. We can solve the above problem by rewriting the getter method of the property.
@property (nonatomic, strong) NSMutableArray *dataSource;
- (NSMutableArray *)dataSource {
if (!_dataSource) {
self.dataSource = [NSMutableArray array];
}
return _dataSource;
}
Especially when encapsulating your own UI controls, when we use the proxy method of the system UI control, we will find that the proxy method will always pass the control object as a parameter. In this method, you can access the properties of the object.
- (void)textViewDidEndEditing:(YYTextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
If your delegate method is only used as a delegate callback for a textView, there is no problem with this type of writing. But if you want to extend your interface, it is likely that another textView will appear in the future interface, then you have to distinguish between the two textViews who are calling this proxy method. At this point, if you have not added the incoming parameter judgment before, then you need to find the name of the previous textView variable, and transfer the previous logic to an if branch before processing the newly added textView logic. Your thoughts are likely to be interrupted. To make matters worse, it is very likely that your little friend is doing this. So it is a good habit to add parameter validation before the extension.
- (void)textViewDidEndEditing:(YYTextView *)textView {
if (textView == self.textView) {
self.navigationItem.rightBarButtonItem = nil;
}
}
1. attribute modifier using caveats In the arc, attributes other than the basic variable weak weak reference is preferably used, to avoid wild pointer, weak automatically set to nil when the object po...
Connect the picture button, Declare method to connect six buttons at the same time -(void)move:(UIButton *)btn{ // NSLog(@"Saw a beauty"); &nbs...
Official issues have also been optimized on the performance of off-screen rendering: IOS 9.0 UIImageView with uibutton Setting rounded angles will trigger off-screen rendering. iOS 9.0 After the UIBut...
UITableView (TableView) is a very universal component in iOS applications, many interfaces can be implemented directly using TableView. But I have seen many friends to implement TableView's DataSource...
1. Try to use the final modifier. Classes with the final modifier are not derivable. In the JAVA core API, there are many examples of final applications, such as java.lang.String. Specifying a final f...
[b]1. Loop optimization[/b] lack: This way will always execute the alist.size () method, resulting in performance consumption, changed to [b]2. Do not create objects in the loop[/b] lack; This practic...
Next, let's review the contents of the previous chapter and run the Neuron_Network_Entry.py program of Create_AI_Framework_In5Classes(Day4) again. The result is as follows: In the results of th...
1, define variables and use Sometimes some color code will be used multiple times in the entire page, then extract it, declare it as a variable, use the variable as a color in css, if there is any nee...
1. Skills in using classes and objects 1. Use new as little as possible to generate new objectsWhen creating an instance of a class with new, all constructors in the rain chain are automatically calle...
Common code optimization (1) 1. Data calculation: use basic data types instead of packaging Result analysis The object declared by Integer will regenerate a new object after each operation Analogy: St...