Objective-C Programming for Android to iOS

Source: Internet
Author: User

Objective-C Programming for Android to iOS

During work, I found that iOS development is also very interesting when chatting with colleagues who are working on iOS. So I want to learn iOS development. My colleagues recommended this book, which is Objective-C, because I have developed Android before, I will be familiar with the syntax habits and usage habits of Objective-C to see if there is anything in JAVA, at the same time, I took some notes, because I had the original Android Foundation, so I did not write down some of the same ones. It is suitable for converting JAVA to Objective-C, just getting started, if you want to learn by yourself, take a good look at this book ---- Objective-C program design.

1. keyboard input:
Scanf (% I, & number );
2. the system automatically implements the get and set methods.
In the interface section, @ property int numerator and denominator
In the implementation section, @ synthesize numerator and denominator
In this way, you do not have to write the get and set methods on your own, and the operation is efficient, and you do not need to declare variables;
3. Methods with multiple parameters
-(Void) setTo: (int) n over :( int) d;
-(Int) set :( int) n :( int) d; // method without parameter name
4. Pass the class object as a parameter
-(Void) add: (Fraction *) f; // The Class Object is referenced.
5. Declaration class reference
@ Class XYPoint; it is just a simple description that XYPoint is a class and no error will be reported in subsequent references. However, if the XYPoint method is used, # import is still used;
6. id dataValue;
Complex * c = [[Complex alloc] init];


DataValue = c;
DataValue can call all Complex methods and determine the methods to be dynamically called at runtime.

[C isMemberOfClass: [Complex class]; // checks whether c is a Complex member.
[C isKindOfClass: [Complex class]; // determines whether c is a member of Complex or its subclass.
[C respondsToSelector: @ selector (setReal: andImaginary :)] // c Response setReal: andImaginary: Method
[Complex instancesResponseToSelector: @ selector (setReal: andImaginary :)] // whether Complex responds to setReal: andImaginary: Method
[C producer mselector @ selector (setReal: andImaginary :)] // execute setReal: andImaginary: Method


7. define a global variable and an external variable
Declare global variables in addition to all methods, classes, and function definitions
Use the extern + variable to use or change the value of the global variable.
Define a global variable but not an external variable
Use static to define the variable. Only the part after the variable is defined in this file can be used. Other files cannot.
8. Assign another name for the Data Type
Typedef int Counter;
Counter j;
Typedef Number * NumberObject;
NumberObject myValue;
Number * myValue;
9. Category
Classes can be modularized into groups or categories of related methods. Provides a simple way to expand existing classes, without the need to parse the source code of the class or create subclass

Create category interface Section
@ Interface Fraction (MathOps) // The classification name is MathOp, which extends the Fraction
How to Create a category
@ Implementation Fraction (MathOps)
Class extension-(unnamed category) @ interface Fraction () // No Name is specified
Compared with the naming classification, you can define additional instance variables to extend the class, which is not allowed in the naming class, and the declared method must be implemented in the main implementation area, cannot be implemented in the separation implementation area
10. Protocol
List of methods shared by multiple files. Only method names are available and are not implemented. These are implemented in classes that comply with this Protocol. -- Similar interface
@ Protocol name
@ Optional // select the implementation method
@ Required // method to be implemented
Compliance Protocol Class Name: NSObject <protocol name>
Determine whether to comply with a certain protocol [object conformsToProtocol: @ prot (protocol name)]
11. Block
// Block Definition
^ (Void)
{
NSLog (@ programing is fun );
}
// Block usage
Void (^ printMessage) (int) =
^ (Int n)
{
NSLogin (@ Program is fun % I, n );
}; // The Block definition ends;
PrintMessage (2 );
13. Access the Foundation Documentation Help-Documentation in xcode
When a required item is displayed, press option and click the mouse
14. in the NSArray array, only objects can be placed. Basic data types (int, float, and so on) cannot be used. Therefore, NSNumber should be used for numbers, and data should be placed in NSNumber ([NSNumber numberWithInteger: 1]), put in NSArray
15. Subclass of variable string NSMutableString NSString
16. Immutable array NSArray and variable array NSMutableArray (the length of the array is variable)
17. Dictionary
NSMutableDictionary-variable dictionary
NSDictionary-enumeration Dictionary (unchangeable. during initialization, dictionaryWithObjectsAndKeys lists all key values)
It ends with nil at creation.
18. File
NSFileManager-create, read, write, rename, delete, exist, attribute, copy, and whether the content of the two files is the same
NSFileManager * fm = [NSFileManager defaultManager]; // Initialization
NSData is used to set the buffer, read the file content into the buffer, or write the buffer content into the file
NSData * fileData;
FileData = [fm contentsAtPath: @ file1name]; // read the content in the file1name file to fileData.
[Fm createFileAtPath: @ file2Name contents: fileData attributes: nil] // write the content in fileData to file2name


NSFileHandle-opens, reads and writes a file, searches for a specified location, reads or writes a specified number of bytes.
NSURL
DataWithContentsOfURL (dictionaryWithContentsOfURL, arrayWithContentsOfURL) can be used to read data of any type on the website)

NSBundle
When creating an application, the system stores the relevant data in the application package and uses NSBundle to access these resources.
[[NSBundle mainBundle] pathForResource: @ filename ofType: @ txt];
[[NSBundle mainBundle] pathsForResourcesOfType: @ jpg inDirectory: @ birdImages];
(/) Indicates the root directory.
(~) Indicates the user's main directory ~ /Filename
19. Copy
Compliance Protocol
Directly copy "=". Two variables direct to the same object.
Use
[Array copy] (Shortest copy) copies another copy, but still points to the same address.


20. Archiving
WriteToFile: atomically: // archive data to a file. You can use arrays, NSString, NSData, and other objects to send messages,
DictionaryWithContentsOfFile: // reads data to the object that sends the message.

[NSKeyedArchiver archiveRootObject: toFile:] // archive content to a file
[NSKeyedUnarchiver unarchiveObjectWithFile:] // read the content in the file
// If an Array is saved above, the Array is read. What type is saved and what type is read?

Custom objects must comply
Protocol, encodeWithCoder, and initWithCoder Methods

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.