and order relatedObjective-C class from the declaration and implementation files m h file, all of the public methods are declared in the h file, private methods can be written in the m file, but the method requires attention in order to compile the early environment, such as the following code, the compiler environment will give early warning:
In h header file, class and method declarations:
@ Interface ObjcNewFeatures: NSObject
- (Void) doSomething: (NSString *) text;
@ End
In M implementation file to achieve:
@ Implementation ObjcNewFeatures
- (Void) doSomething: (NSString *) text {
NSLog (@ "% @", [text stringByAppendingFormat: [selfgetCode]]);
}
- (NSString *) getCode {
return @ "Unicode";
}
@ End
In the early compiler to compile, will issue the following warning:
warning: instance method '-getCode:' not found ...
This is because, according to the order of the compiler, the compiler does not know there getCode after doSomething method, it will be given a warning. There are a variety of solutions, such as can be put before the getCode method doSomething, you can declare private methods in advance, as follows: Increase getCode method m file, like this:.
@ InterfaceObjcNewFeatures ()
- (NSString *) getCode;
@ End
But from Xcode4.6 start, the new compiler adds new features, change the order of the compilation approach, first scanning method declaration, and then compile them to achieve some. So whether it is public or private methods, it becomes irrelevant order it.
The latest version of XCode 5.0 using default compiler is Apple LLVM compiler 3.1, the above code to run properly under the latest build environment.
Objective-C using the new version, you can write more concise code, while avoiding some of the common pitfalls. More importantly, these features are fully backward compatible syntax, the use of the new features of the code written out, after the formation of a binary compiled program can run on any OS prior to publication.
Recommendation 2: Note that the enumeration of recent energy-absorbing improvements
Enumeration before OS X v10.5, how do we define an Objective-C enum types? As follows:
typedef enum {
ObjectiveC,
Java,
Ruby,
Python,
Erlang}
Language;
The wording is simple, it is not complicated to use, but there is a problem is the scope of its enumerated value of the data is ambiguous, this value may be very large, it may be negative, can not be defined.
After OS X v10.5 and iOS, you can write:
enum {
ObjectiveC,
Java,
Ruby,
Python,
Erlang
};
typedef NSUInteger Language;
The benefits of such an approach is that, first of the enumerated data type is determined, unsigned integer. Second, because we used a NSUInteger, can not consider 32-bit and 64-bit problem. The problem is caused by data type and no explicit enumeration constants associated.
In XCode4.4, you can write enumerates:
typedef enum Language: NSUInteger {
ObjectiveC,
Java,
Ruby,
Python,
Erlang
} Language;
Enumerate the contents of the list, while the binding of the enumerated data type NSUInteger, bringing the benefits of this type of inspection is to enhance and better code readability.
Of course, this is for the average developer, enumerated types may not involve complex data, the use of two written before there will not be a big problem. Anyway, after XCode4.4 release, you can try out the new wording.
Objective-C using the new wording, you can write more concise code, while avoiding some of the common pitfalls. More importantly, these features are fully backward compatible syntax, the use of the new features of the code written out, after the formation of a binary compiled program can run on any OS prior to publication.
Recommendation 3: The property has an automatic synthesis features
Each developer of the property are familiar with, you need to define the attributes of the class, write getter and setter methods. So in Objective-C is how to deal with attributes? Very simple, first define attributes in h header file:.
@ Property (strong) NSString * name;
M implementation file and then use the @ synthesize directive to achieve accessor methods and instance variables defined attributes ivar:.
@ Synthesize name = _name;
@ Synthesize the implication is that the case if not overloaded, the compiler will automatically generate getter and setter methods _name class instance variables based on read-write property. Of course, the related methods of this property can also be specified by the developer to achieve with @ dynamic directive.
It looks very simple is not already? Only more simple but not easy.
In XCode4.4, you can omit the @ synthesize name = name; this line, entirely to the compiler to be realized. That statement in the name attribute. H header file, you can use directly in the implementation file for the property getter and setter methods, and use the instance variable name. And the compiler will automatically determine in accordance with readable and writable property setter methods are provided.
So in this case, if the property is declared @ dynamic compiler how to handle it? All synthesize related features will no longer work, so you need to realize related methods attributes.
Then click Properties total synthesis of new features:
l Unless explicitly stated otherwise, the properties associated accessor methods (getter and setter) will be generated automatically.
l unless all accessor method provides instance variables, otherwise the instance variables (such as _name) will be generated automatically.
l If you use @ synthesize, does not provide the strength of the variable name, it will be automatically generated.
l If you use the @ dynamic, then the automatic synthesis invalid, who need to develop their own implementation.
l Core Data's NSManagedObject and its subclasses do not use the default attribute synthesis.
Recommendation 4: Learn to write code with syntax simplifies
Many just another programming language Objective-C students to see the long function names will feel crashes, this syntax allows transmission of messages like an English sentence, greatly enhancing readability. For example, want to initialize a float, so need to write:
NSNumber value = [NSNumber numberWithFloat: 123.45f];
From this sentence we can clearly know the meaning of the code, however, whether even such a simple assignment statement should deal with it? In this new feature Apple adopted a compromise approach, the basis for many types using a shorthand way to achieve simplified syntax. After simplification, you will find from the syntax level, these simplified Objective-C is more like Python and Ruby dynamic language grammar.
Following elaborate:
1.NSNumber
Simplified wording before:
NSNumber * value;
value = [NSNumber numberWithInt: 12345];
value = [NSNumber numberWithFloat: 123.45f];
value = [NSNumber numberWithDouble: 123.45];
value = [NSNumber numberWithBool: YES];
Simplified wording:
NSNumber * value;
value = @ 12345;
value = @ 123.45f;
value = @ 123.45;
value = @ YES;
Similar expressions can also be used packing wording:
NSNumber * piOverSixteen = [NSNumber numberWithDouble: (M_PI / 16)];
NSString * path = [NSString stringWithUTF8String: getenv ("PATH")];
Can respectively abbreviated as:
NSNumber * piOverSixteen = @ (M_PI / 16);
NSString * path = @ (getenv ("PATH"));
For string expression, it should be noted that the value of the expression must not be NULL, otherwise it will throw an exception.
2. NSArray
For NSArray initialization, there are a lot of writing, there is no longer have set directly see the new wording
NSArray * array;
array = @ []; / / empty array
array = @ [a]; array
/ / an objectarray = @ [a, b, c]; array
/ / multiple objectsVery simple, no longer have to remember when initializing an array of multiple objects, but also with a bad back nil. Now look at the array declaration when the multi-object compiler is how to deal with:
array = @ [a, b, c];
The compiler generated code:
id objects [] = {a, b, c};
NSUInteger count = sizeof (objects) / sizeof (id);
array = [NSArray arrayWithObjects: objects count: count];
Well, the compiler has to put these simple repetitive tasks are done, you can feel at ease to solve the real problem :) But it must be noted that if a, b, c object has nil, then the runtime will throw an exception This point and the original deal in different ways, be careful when coding.
3. NSDictionary
Again, this data structure for the dictionary, there are many ways to initialize, run the new wording:
NSDictionary * dict;
dict = @ {}; / / empty dictionary
dict = @ {k1: o1}; / / contains a key-value pair dictionary
dict = @ {k1: o1, k2: o2, k3: o3}; / / contains a number of key-value pairs in the dictionary
Finally, the total container then what type of data structure simplifies limitations: a container constructed using the above wording is immutable, if you need to generate a variable container, you can pass-mutableCopy message. For example
NSMutableArray * mutablePlanets = [@ [
@ "Mercury", @ "Venus", @ "Earth",
@ "Mars", @ "Jupiter", @ "Saturn",
@ "Uranus", @ "Neptune"
] MutableCopy];
Can not be directly assigned to the constant array, the solution is in a class method (void) initialize assignment, as follows:
@ Implementation MyClass
static NSArray * thePlanets;
+ (Void) initialize {
if (self == [MyClass class]) {
thePlanets = @ [
@ "Mercury", @ "Venus", @ "Earth",
@ "Mars", @ "Jupiter", @ "Saturn",
@ "Uranus", @ "Neptune"
];
}}
To note: there is no constant dictionary
Simplified using Objective-C syntax, you can write more concise code, while avoiding some of the common pitfalls. More importantly, these features are fully backward compatible syntax, the use of the new features of the code written out, after the formation of a binary compiled program can run on any OS prior to publication.
Recommendation 5: to get as far as possible with the subscript arrays and dictionary objects
Let us simplify the syntax is not difficult to think of a container, you can access the data through arrays and dictionaries subject under way. For example, for the array:
NSArray * array = @ [a, b, c];
Can be written:
id obj = array [i]; / / get the subscript way through an array of objects, replacing the original wording: array objectAtIndex: i];
array [i] = newObj; / / can also be assigned directly to an array of objects. Replace the original wording: [array replaceObjectAtIndex: i withObject: newObj];
For the dictionary:
NSDictionary * dict = @ {k1: o1, k2: o2, k3: o3};
Can be written:
id obj = dict [k2]; / / get o2 object, replacing the original wording: [dic objectForKey: k2];
dic [k2] = newObj; / / re-k2 for the key object to the assignment, replacing the original wording: [dic setObject: newObj forKey: k2]
Meanwhile, the container class define yourself, as long as the realization of the provisions under the standard method, you can access the data using the subscript. To achieve the following:
Array subscript type method
- (ElementType) objectAtIndexedSubscript: (indexType) idx;
- (Void) setObject: (elementType) object atIndexedSubscript: (indexType) idx;
Dictionary type index method
- (ElementType) objectForKeyedSubscript: (keyType) key;
- (Void) setObject: (elementType) object forKeyedSubscript: (keyType) key;
Note that indexType which must be an integer, elementType and keyType must be an object pointer.
Using Objective-C, you can get these new features, write more concise code, while avoiding some of the common pitfalls. More importantly, these features are fully backward compatible syntax, the use of the new features of the code written out, after the formation of a binary compiled program can run on any OS prior to publication.
Excerpt from Liu a "writing quality code: 156 recommendations to improve Objective-C program"
2014 officially listed
Liu an "iOS 7: iPhone / iPad application development techniques explain," in November 2013 has been listed
Reply:
Reply:
Top
Reply:
Reply:
These elements are Apple's Objective-C, part of the latest improvements and improved
Reply:
Reply:
1. Immediately begin developing applications series http://download.csdn.net/detail/langyifei/6590585, note that the material is in English
2 adventure game since the game code to reach more than 60 M, can not upload. So need to book e-mail address to obtain
Use Sprite Kit (Kit) toolkit to easily create compelling, high-performance gaming principle, iOS and OS X use the same object-oriented design patterns as well as other applications that you use to generate the Objective-C language and the language. In order to prove that you can accomplish with Sprite Kit (Kit) toolkit, we have created a complete adventure game:
This code: Xcode project through exploration, introduction and overview of the boot. Through this document, you will see how we can use Sprite Kit (Kit) Set node, action and physical, to establish a level, you play through the walls of a brave hero of the forest maze. There are a few different characters and basic spirits, make up games, including your hero, along the way you will encounter evil goblin cave spawning, not to mention the level of the club head waving at the end.
3 based on non-Mac platform to build Xcode 5 development environment, http://download.csdn.net/detail/langyifei/6599225
For "iOS 7: iPhone / iPad application development techniques explain," due to personal mistakes, updates and changes in the manuscript, because of busy, forget to update the contents of this section 1.3, causing the reader to give you a big not convenient, because of space problems, books for the introduction iOS 7, some less. After the discovery of the readers of these two issues, I pay close attention to modify the contents, in order to compensate for the individual's fault, the author of the blog published a large number of individuals latest iOS 7 official technical documentation materials and examples to get readers can expect requests for your understanding. Meanwhile personal iOS also deliberately opened a technical exchange group (212 325 258), hoping to provide the author of the reach of power can help our readers and seniors.
4. IOS7 new features http://blog.sina.com.cn/s/blog_4ca9ceef0101hnpo.html
. 5 iOS 7: User Interface Transition Guide http://blog.sina.com.cn/s/blog_4ca9ceef0101hjqo.html
6 breakpoints and debug http://blog.sina.com.cn/s/blog_4ca9ceef0101hnq2.html
Xcode conducted. 7 iOS 7: Human-Computer Interaction Guide - UI design based on http://blog.sina.com.cn/s/blog_4ca9ceef0101hjr0.html
8 iOS 7:. Human-Computer Interaction Guide - UI design basis under http://blog.sina.com.cn/s/blog_4ca9ceef0101hjrz.html
. 9 iOS 7 Human-Computer Interaction Guide - Design Strategies http://blog.sina.com.cn/s/blog_4ca9ceef0101hjsp.html
10. IOS 7 Guide-UI elements in human-computer interaction design-Bar http://blog.sina.com.cn/s/blog_4ca9ceef0101hjt8.html
11.iOS 7 Guide-UI elements in human-computer interaction design - the content view http://blog.sina.com.cn/s/blog_4ca9ceef0101hjtb.html
. 12 iOS 7 Guide-UI elements in human-computer interaction design - Controls http://blog.sina.com.cn/s/blog_4ca9ceef0101hjte.html
. 13 iOS 7 Guide-UI elements in human-computer interaction design - temporary view http://blog.sina.com.cn/s/blog_4ca9ceef0101hjtf.html
Reply:
There is a doubt with a subscript contrast objectAt What are the advantages of it?
Reply:
You can simplify the code, do not write so much, huh. . . Easy to use these
Reply:
Reply:
Reply:
Very good very good very good,
Reply:
Reply:
Reply:
Reply:
Well, good post
Reply:
No comments:
Post a Comment