1. The difference between struct and class
In Swift : class is a reference type. A struct is a value type . Value types are copied when they are passed and assigned. A reference type uses only one "point" of the Reference object. So the difference between the two is two types.
Class has these few functions that the struct does not have:
- Class can inherit. This way the class can use the attributes and methods of the parent class
- Type conversions can check and interpret the type of an instance at runtime
- You can use Deinit to release resources
- A class can be referenced multiple times
Structs also have several advantages:
- Smaller structure. Applies to copy operations. Multiple references are more secure than instances of a class.
- No need to worry about memory leak or multithreading conflict issues
By the way: array is implemented in Swift with a struct. Apple has rewritten an array once. Then copy is the deep copy. guessing that replication is similar to a reference. Implemented by pointers to the position on the heap on the stack. And for its copy operation. It is also done in a relatively well-proportioned heap. So the performance is still good.
C language : The difference between struct and class:
A struct is simply defined as a complex data type. cannot be used for object-oriented programming.
C + + : The difference between struct and class:
For member access rights and how they are inherited. The default in class is private. And the struct is public. Class can also be used to represent template types. struct is not.
2. Introduce the Observer pattern
Observer pattern [Observer pattern]: Defines a one-to-many dependency between objects. Each time an object state changes. Its dependent objects are notified and automatically updated. The
typical push model implementations in iOS are Nsnotificationcenter and KVO.
Nsnotificationcenter
- Watcher Observer: Addobserver:selector:name:o through Nsnotificationcenter Bject interface to register an interest in a type of notification. Be sure to pay attention when registering. Nsnotificationcenter does not do a reference count of +1 for the Observer. When we release the Observer in the program. Be sure to report it off from center. The
- notification hub nsnotificationcenter. Notification hub.
- The object that is being observed. By PostNotificationName:object:userInfo: Sends a notification of a type. Broadcast changes. The
- notifies the object nsnotification. When a notification comes. The center invokes an interface registered by the observer to broadcast the notification. The Nsnotification object that stores the changed content is also passed. The full name of the
KVO
KVO is Key-value Observer. The key value observation. is a way of realizing the observer pattern without a central hub. A Subject object manages the observer objects that depend on it. and proactively notifies the observer object when its state changes.
- registered Observer [object Addobserver:self Forkeypath:property options:nskeyvalueobservingoptionnew context:]. The
- changes the value of the Theme object property. That is, the notification that triggered the change. The
- is in the callback function that was developed. Processes the notification of changes received.
- unregister Observer [Object Removeobserver:self Forkeypath:property].
3. On an HTTPS-connected website. Enter your account password and click Login. Before the server returns this request. What's going on in the middle?
- The client packages a request that includes a URL, a port, an account password, and so on. The account password login should use the post mode. So the relevant user information will be loaded into the body inside. This request should contain three aspects: Network address, Protocol, resource path. Note: This is HTTPS. is HTTP + SSL/TLS. Another layer of encryption information is added to HTTP [equivalent to a lock ].
- The DNS server is typically requested first. The DNS server is responsible for resolving your network address to an IP address. This IP address corresponds to a machine on the web. This may occur with the hosts hijack and ISP failure issues.
- The protocol is the way to get resources for HTTP, FTP, and UDP. Different protocols have different formats. Some of them are process-to-process. Some of them are host-to-host.
- A socket connection is established between the client and the server's port. Sockets generally parse requests in the form of file descriptor.
- The server side receives the request. The server side will have a set of digital certificates [equivalent to a key ]. This certificate is returned to the client first. The client resolves the certificate. The equivalent of using the key [certificate] to lock [the contents] [the creation of a private key]. The encrypted message is then transmitted.
- After the server side receives the encrypted information [private key]. will be decrypted. And the data to be returned is symmetric encrypted back to the client. If the path is not correct. A 404 error will occur.
- A proxy may be accessed before the general access server. This thing is a proxy. Sometimes when firewalls are used. Sometimes when the cache makes. If the background is a reverse-proxy structure. Then there are actually multiple Web servers hidden behind the proxy and processing requests on demand. And you always visit the proxy. This will solve the overload problem.
- Sometimes you have to access the file server after you have finished accessing the Web server. The main request is some information in the database.
- The server will be packaged accordingly. Return to the client directly or through proxy[most of the time]. The client decrypts with the private key that was just generated. Displays content on the browser.
- HTTPS: Encryption Process Details click
4. There is a button in the middle of an app. After you touch the screen in your hand, click the button to receive the Click event. What happened in the middle?
There are several steps in the response chain:
- The Uitouch and Uievent objects that the device will touch to package. In the event queue of the currently active application
- The uiapplication of the Singleton takes the touch event out of the event queue and passes it to the singleton UIWindow
- UIWindow using the Hittest:withevent: method to find the view in which the touch operation is located
Runloop
- The runloop of the main thread is awakened
- Notify Observer. Handling timer and source 0
- Springboard to app process after receiving touch event
- Runloop processing source 1, Source1 triggers a callback. and call _uiapplicationhandleeventqueue () for distribution within the app.
- Runloop finished processing to sleep. Previously, the old Autorelease pool was released and a new autorelease pool was created
Uiresponder is the parent class of UIView. UIView is the parent class of Uicontrol.
iOS face questions