在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jverkoey/iOS-Best-Practices开源软件地址:https://github.com/jverkoey/iOS-Best-Practices开源编程语言:开源软件介绍:Best Practices for iOS Software DesignThis article's goal is to help you write stable code for your iOS applications. I highly encourage you to contribute your own best practices via Github's pull requests.
Originally written by: Jeff Verkoeyen (@featherless) Table of ContentsBe Mindful of the Lifetime of Views
Do not access self.view in init- methodsYou should never access Consider a simple example: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
self.view.backgroundColor = [UIColor underPageBackgroundColor];
}
return self;
} Imagine this controller is the root of a navigation stack and a memory warning occurs. When we pop back to this
controller, the view will no longer have Use data source protocols to strongly separate data from viewsWhen designing views that interact with data sets, always fetch the data via a data source protocol rather than exposing setters. Views are not data containers and should not be designed to enable any such abuse. Rather, views should be treated as expendable components that may leave existence at any point in time. As a general rule of thumb, anything beyond static presentation information in a view should be requested via a data source or delegate. UILabel is a good example of a view that does not need a data source. All of its properties are set once and are generally not expected to change throughout the lifetime of the view. UITableView on the other hand requires a data source to fetch its data. Let's imagine what using UITableView would be like if it didn't have a data source and instead only provided setters. This design will lead to inevitable abuse when developers attempt to use the table view object as a place to store their data. When the table view is inevitably released due to a memory warning the data will also be lost! This means that we need to store the data elsewhere anyway in order to guarantee its lifetime across multiple instances of the table view. UIViewController
Use the existing navigation item objectEvery instance of a UIViewController has a // UIViewController will automatically create the navigationItem object.
self.navigationItem.rightBarButtonItem = doneButton; NSObjectOnly expose public properties and methods in headersObjective-c allows you to define private properties in a category interface within your This is equivalent to defining ivars as ExampleViewController.h @interface ViewController : UIViewController
@property (nonatomic, readonly, assign) NSInteger objectId;
@end ViewController.m #import "ViewController.h"
@interface ViewController()
@property (nonatomic, readwrite, assign) NSInteger objectId;
// Notice that this property doesn't need to be in the .h. Objective-C will create this
// property on the fly!
@property (nonatomic, readwrite, retain) UILabel* objectLabel;
@end
@implementation ViewController
@synthesize objectId;
@synthesize objectLabel;
...
@end DebuggingUse lldb for debugginglldb allows you to inspect properties on classes that don't have explicit ivars declared in the object's interface. To use lldb, select "Edit Scheme..." from the "Product" menu (or press Cmd+Shift+<). Select the "Run" tab on the left-hand side of the scheme editor. Change the debugger drop down to "LLDB". Use NSZombieEnabled to find object leaksWhen NSZombieEnabled is enabled, objects that are released from memory will be kept around as "zombies". If you attempt to access the released object again in the future, rather than crashing with very little context, you will be shown the name of the object that was being accessed. This can be incredibly helpful in determining where memory leaks are occurring. To turn on NSZombieEnabled, select "Edit Scheme..." from the "Product" menu (or press Cmd+Shift+<). Select the "Run" tab
on the left-hand side of the scheme editor. Select the "Arguments" tab in that page. Add a new Environment Variable
and call it DocumentationData Source ProtocolsFor required methods, start the preamble with "Tells the data source" and end it with "(required)". For example:
For optional methods, start the documentation with "Asks the data source". For example:
Delegate ProtocolsFor methods that simply notify the delegate that an action has occurred, start the preamble with "Informs the delegate". For example:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论