IOS study notes Objective-C programming BNR (Second Edition)

Today Objective-C programming (Second Edition) this book to do a brief summary of the study.

How to read this book

The first chapter of the book on this issue, according to the author to do it.

IOS study notes Objective-C programming BNR (Second Edition)

Content layout (the book is divided into 6 parts)

The first part: Introduction

As the saying goes, a good beginning is half done. The contents of this part to help me set up the correct learning programming ideas, if not to go swimming, the theory of knowledge more programming can not swim, just like swimming, just look at the theory is to learn good, only knock code, many mistakes and correct mistakes, to master the language.

This section briefly describes a vast majority of programming books will be held in the “Hello World” program examples, and a simple explanation of how the program is running. Is this small program behind also involves a lot of knowledge points especially in the bottom area, but in the present stage is not recommended to go, because I’ve wanted to know more, see understanding the famous –&gt computer system; computer system –> high quality programming guide: C++, C –&gt C program language; –&gt C Primer Plus design language; (Fifth Edition) (Chinese Edition) –> C and –&gt pointer; C programming: a modern approach (Second Edition), which is a very good book, and I have bought these books (although the Internet has electronic scanning version, but in order to improve their learning initiative is to facilitate their learning, I spend money to buy, improve the learning cost, I that I must learn that money is not easy!) But I will deviate from the main purpose of my study and master the Objective-C language, and the front stretch, learning is very tortuous, very hurt my enthusiasm, I also read these books very shallow, the harvest is not, after all, every one of the books here can be a semester later, so I return the main line, back to the book study.

By the way on the C language learning experience, it is said to be a tear & tired. Not much to say, I would recommend that he learned to feel good information. For the basic knowledge of a friend who does not recommend the computer (Baidu cloud sharing) C language tutorial – Hao Bin, to explain in great detail, help to grasp the knowledge points, the content is not complicated, conducive to learning enthusiasm. The main learning books recommended C language programming: modern methods (Second Edition), the book on the watercress there is a very good comparison of the evaluation, you can refer to the next. This is just the beginning of the C language learning materials, I am still more food, but to learn C language I think it is conducive to the future of programming. After all, how to emphasize the basic knowledge is not too much!

The second part: how to program

This section describes the need to understand the Objective-C language learning C language content, focusing on the basic data types, control structure, function, pointer, structure.

A function variable is a name associated with a block of data, a function is a name associated with a block of code”. A program can pass information to a function, requiring a function to execute code and return information. When a function is needed, it can be achieved by creating a function. A program is a collection of functions that, when the program is running, the computer will copy these functions from the hard disk to memory, and then find the “main” function and execute. There are local variables in a function that are stored in the frame of a function (frame), which is used by the programmer to describe the locations of these frames in memory (stack). Stack is the first out of the first, the implementation of the function, the function of the frame will be created at the top of the stack, the end of the implementation of the function, the frame will exit the stack, that is, the function returns, waiting for the next call.

Pointer memory is a numeric number, usually with the address (address) to refer to a specific byte of data. When the program is executed on the computer, all the relevant data (variables, functions, etc.) will be stored in memory, and have their own address. Pointers are used to save these addresses, which do not save them, but point to an address, which is the memory address of the value. Why do we need pointers? Because the program does not necessarily transmit data through a copy, it must be able to pass the data directly or through a copy of the start address. Once the start address of the data, you can easily access the corresponding data.

How can you return two or more values by passing a C function by reference only to return a value? Can be achieved through the use of reference: when the function is called to an address (also known as a reference), and then by the function of the data stored in the address to the memory. This is a metaphor in the book: suppose you’re in charge of a spy. One day, you told a spy, “get a picture of the Treasury Secretary and his girlfriend. I’ve got a short steel pipe under the angel statue in the park. After I get the picture, I put it in the steel pipe, and I’ll take it after lunch on Tuesday.” In the jargon of espionage, referred to as the drop (dead). Call the function to be passed to an address, similar to the park’s short steel pipe, so that the function can be stored in the specified location of the results, so that the caller to take.

When a program is written in a heap and heap, a variable may be used to hold a number of related data. C language, we can use the structure (structure) to achieve this function. Each data is referred to as a member of the structure (memeber). In front of the program are used in the stack of memory, this kind of memory space will be called when the function is automatically assigned by the system, and after the end of the function automatically released. This is also the reason why local variables are often called automatic variables. Only automatic variables are not enough, and sometimes “apply” to a contiguous memory buffer (buffer). The buffer is from a specific memory area, the heap (heap), which is separate from the stack. On the heap, the buffer is independent of any function stack. So it can be used in multiple functions. For example, you can declare a buffer storage of text files, and then call a function to the text file to the buffer zone, the number of calls another function to count vowels in the text, then call a function to check. After processing the text file, you can return the memory of the buffer to the heap. The key to the structure of the front of our statement is as (main) local variable function frame on the stack, is the structure on the stack, we now allocate a buffer structure in the heap structure, a statement on the heap. And the structure of this heap is the next we want to learn the realization of Objective-C object foundation. The essence of the Objective-C object is the structure, and the structure on the heap. Think about why you want to use the structure on the heap, rather than using the stack structure? This problem will have an answer in memory management. With the problem of learning can drive themselves to think more to help themselves better understand the concept, such learning is the most effective. Simply put my understanding of this problem: the structure of the stack has two advantages: high speed, the use of the stack is the first level of the cache, so the stack can be quickly allocated memory, while the heap is using a two level cache. 2 simple, the structure of the stack has its own life cycle, remember the previous local variables, the stack structure is the same, when it is beyond the scope of the computer will be automatically destroyed. But it cannot obey the life cycle of ARC, in Obiective-C by automatic reference counting (ARC) to manage memory, each object of a pointer to itself (or reference) count, when the reference number is 0 when they don’t need the object, the program will automatically destroy the object. The structure on the stack or the object function has only one point to their owners that the creation of the stack object, when you apply this object to the other code, you are invalid on his retain, it also cannot be saved, because when the function returns when the creation of the stack structure, stack structure the body will be automatically destroyed, it will produce a dangling reference, cause the program to crash. So the Objective-C object uses a heap structure that is an object on the heap. But there are also the normal use of the stack object, that is the Blocks object.

The third part Objective-C and Foundation

This part is the focus of the book, tells the basic syntax and characteristics of Objective-C, but also on the basic framework of the Foundation in a few commonly used classes.

Objective-C language is a superset of the C language, which is based on the “structure” created from the heap as the theoretical foundation, joined the message transmission mechanism from the Smalltalk language, object-oriented language form.

The object (object) is similar to the structure, but the object contains not only data, but also the instance variable and the function. Class (class) is responsible for describing a particular type of object, including methods and instance variables. Class can describe the following two objects: 1 abstract concepts, such as data, strings, or instruction sets. 2 something, such as a person, a place, or a savings account. A class defines an object, and it can also be used to create such an object. A class is both a prototype of an object and a factory that produces objects. Method and function. It performs the corresponding functions according to instructions. In the Objective-C language, if you need to execute the code in the method, you first need to send a message (you can take parameters) to the object or class that contains the method. Objects can only be accessed by pointers, because objects are on the heap, and you want to declare a pointer variable to hold the address of the object in memory. The stack is made up of an ordered frame, the frame of the function will be released automatically after the function is finished, and the object in the heap will not be released automatically. It needs to be managed by ARC automatic reference counting.

Memory management thinking:
1 generated their own objects, their own.
2 is not the object of their own generation, they can hold.
3 no longer needs to hold its own object when released.
4 object cannot be released by itself.
from the Objective-C Advanced Programming: iOS and OS X multithreading and memory management,

Property (property) is a property of the Objective-C language used to encapsulate data in an object. Attribute simplifies the access method, the object will save the data as an instance variable, and the instance variable is accessed by the access method. Property has 4 types of properties that affect the access method that the compiler generates. These 4 types of features are: (1) atomic (nonatomic), the default is atomic, the property is either atomic or non atomic, the difference with the multi-threaded operation related. I don’t know the details. At present, the use of non atomic. 2 read / write permissions (readwrite and readonly) is the default read and write permissions, which means that the instance variables have stored methods and methods. Property is either read-only or read – write, and read – only is an instance variable. 3 memory management semantics (assign, strong, weak, unsafe_unretained, copy) because the data has “specific ownership semantics”. This set of features will only affect the setting method”. For example, when you set a new value with the setting method, should it “keep” this value? Or just assign it to the underlying instance variables? 4 method names (getter=< name> and setter=< name>) this method can be used to specify the access method.
extracted from the “Effective C 2.0: X high quality iOS and OS code to prepare 52 effective methods”, “Objective

Inheritance (inheritance) inheritance is also a feature of the Objective-C language, an instance of the subclass can inherit all the methods of the parent class. Usually subclasses and parents are different, you can change the inheritance (override) to inherit from the parent class method, divided into all changes and partial change (super). Root NSObject has many methods, but only one instance variable: isa pointer. The ISA pointer of any object points to the class that created it. When a message is sent to an object, the object is inquired about the method of the message name. The search will find the class of the object through the ISA pointer and query whether the instance method is known as the name of the message” If not, it will continue to query its parent class. By analogy, the object will go up the inheritance chain until it finds a method called the message, or arrives at the top of the inheritance chain (NSObject).

Object instance variables and attribute instance variables can be pointers to other objects. Here are three common uses of object instance variables. 1 object property: pointer to an object that represents a value. 2 one to one relationship: pointer to a single complex object. 3 one to many relationships: pointer to an instance of a colllection class.

Strong reference and weak reference object ownership: when an object (A) has an object instance variable (B), it is called the A object with B. The relationship between things is bidirectional, so the ownership relationship between objects can cause the related objects to be released. This is a common cause of memory leaks. The strong reference cycle problem can be solved by using weak references. Weak reference does not specify the ownership of the pointer, that is, the property of the memory management semantic class is set to weak. If the object is a parent child relationship, the parent object has a child, but the child does not have a parent object. A strong reference retains the owner of the object so that it will not be released. The weak reference will not be retained, so it is possible that the weak reference instance variable and the attribute pointing object may disappear. If this happens, then the instance variable or property is set to nil, without losing the point to point to the object.

A callback (callback) callback is to bind a piece of executable code to a specific event. When the pending event occurs, the code is executed. In the Objective-C language, there are four ways to achieve callback. 1 target action pair (target-action): before the program begins to wait, it is required to send a specific message to the specified object when the event occurs”. The object that receives the message here is the target (target), the message selector (selector) is the action (action). 2 auxiliary helper (objects): before the program begins to wait, “when the event occurs, the corresponding protocol to comply with the corresponding object to send messages”. Delegate objects (delegate) and data sources (data source) are common secondary objects. 3 notification (notification): before the program begins to wait, you can inform the notification center that an object is waiting for a specific notification. When a notification occurs, a specific message is sent to the specified object”. When an event occurs, the associated object will issue a notification to the notification center, which will then be forwarded to the object waiting for the notification by the notification center. 4.Block object (blocks): Block is an executable code. Remember the objects on the stack? It is the. Before the program starts waiting, declare a Block object that executes the Block object when the event occurs.

Protocols: who are you and what you do is a different concept, the object is the same: in a work system, the object of the class and the object to play the role is different. Protocol can specify roles for an object. A protocol is a declaration of a set of methods, some of which are required, and others are optional. If the object is to play a specific role, it is necessary to implement the necessary methods, and choose to achieve some of the optional methods.

The fourth part is the application of event driven

This part of the realization of the two simple applications are iOS applications and Cocoa applications. Xcode version of the book is 5, the latest version of the 7.3 version of the comparison of the larger changes, but it is not affected by the book in accordance with the study of these two cases in the case of the 5.

The fifth part Objective-C advanced topics

This part of the five points of knowledge: init method, to further explain the properties of KVC (key-value coding), KVO (key-value observing), category (Categories). This book is a simple explanation, but also need to deepen the understanding of the next instance of learning.

The sixth part of the C Language Advanced Topics

This part also talked about five knowledge points: bit operation, C string, C array, from the command line to run, Switch statement.

Concluding remarks

At this point, this book will be finished, but this is only the beginning, the road to go behind is still very long, we must learn the language foundation, the road back to go, so this book I would also like to visit.

IOS study notes Objective-C programming BNR (Second Edition)

This article wrote a little bit about their own Objective-C is still relatively superficial understanding, if there is a wrong place, please correct me! Thank you

The last book on the book (Baidu cloud) English version of it, English is not very good or recommend to read the Chinese version.

RUNSA gold! Come on.