Li Hongqiang iOS Classic face question

Source: Internet
Author: User

    • Li Hongqiang iOS Classic face question

1. The difference between struct and class

In Swift ,class is a reference type, and a struct is a value type . Value types are copied when they are passed and assigned, whereas reference types only use one "point" of the referencing object. So the difference between the two is two types.

Class has these few functions that the struct does not have:

    • Class can inherit, so that classes 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:

    • Small structure, suitable for copy operations, is more secure than multiple references to 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 rewrites an array once, and then copy is a deep copy. guessing that replication is similar to a reference is achieved by pointers to the positions on the heap on the stack. And for its copy operation, but also in the relatively more generous heap on the completion of, so the performance is still good.

The following is a reference to the cat God Onev Blog:

var arr = [0,0,0]

var newArr = arr

Arr[0] = 1

Check arr and NEWARR

Arr//[1, 0, 0]

NEWARR//Before beta3:[1, 0, 0], after beta3:[0, 0, 0]

So it is possible to guess that the behavior behind the array and dictionary is not as simple as other structs on the stack, but rather as a reference, by pointers to the position on the heap on the stack. And for its copy operation, it is also in relatively more space on the heap to complete. Of course, it's impossible (or difficult) to get the final remittance code, so it's just a guess.

Add:

C language , the difference between struct and class:

Structs are simply defined as a complex data type and cannot be used for object-oriented programming.

In C + + , the difference between struct and class:

For member access and inheritance, the default in class is private, and the struct is public. Class can also be used to represent template types, but not for structs.

2. Introduce the Observer pattern

Observer pattern (Observer pattern): Defines a one-to-many dependency between objects, so that whenever an object state changes, its dependent objects are notified and automatically updated.

Typical push model implementations in iOS are Nsnotificationcenter and KVO.

Nsnotificationcenter

Nsnotificationcenter

    1. The Observer, Observer, is interested in registering a type of notification through the Nsnotificationcenter AddObserver:selector:name:object interface. When registering, it is important to note that Nsnotificationcenter does not perform a reference count of +1 to the observer, and when we release the Observer in the program, we must write it off from the center.
    2. Notification hubs Nsnotificationcenter, notification hubs.
    3. Observed objects, via PostNotificationName:object:userInfo: Send a notification of a type, broadcast change.
    4. Notifies the object nsnotification that when a notification comes in, center invokes an interface registered by the observer to broadcast the notification while passing the Nsnotification object that stores the change.

KVO

The full name of KVO is Key-value Observer, which is the key value observation. is a way of realizing the observer pattern without a central hub. A Subject object manages the Observer object that is dependent on it and proactively notifies the observer object when its state changes.

    1. Registered Observer
    2. [Object Addobserver:self forkeypath:property options:nskeyvalueobservingoptionnew context:].
    3. Changes the value of the Subject object property, which is the notification that the change is sent.
    4. In the developed callback function, process the notification of the change received.
    5. Unregister Observer [Object Removeobserver:self Forkeypath:property].

3. In an HTTPS connected Web site, enter the account password click Login, before the server returned to the request, what happened in the middle

This is very deep and very wide, I would like to say a little bit about it.

HTTPS encryption process

  1. The client packs a request, including the URL, port, your account password, and so on. Account password login should use the Post method, so the relevant user information will be loaded into the body inside. This request should contain three aspects: Network address, Protocol, resource path. Note that this is HTTPS, HTTP + SSL/TLS, and a layer of encryption information is added to the HTTP (equivalent to a lock ).
  2. The DNS server is typically requested first. The DNS server is responsible for resolving your network address to an IP address, which corresponds to a single machine on the web. This may occur with the hosts hijack and ISP failure issues.
  3. protocol is the way to obtain resources HTTP,FTP,UDP, different protocols have different formats, some are process-to-process, some are host-to-host.
  4. The client establishes a socket connection to the server's port, and the socket generally resolves the request in the form of file descriptor.
  5. The server side receives the request. The server will have a set of digital certificates (equivalent to a key ) that will be returned to the client first. The client resolves the certificate, which is equivalent to locking the lock (the content) with the key (the certificate) and then transmitting the encrypted information.
  6. After the server receives the encrypted information (private key), it decrypts and returns the data to the client with symmetric encryption. If the path is not correct, a 404 error will occur.
  7. A proxy may be accessed before the general access server. This thing is a proxy, sometimes when the firewall is used, sometimes when the cache makes. If the background is a reverse-proxy structure, then there are actually multiple Web servers hidden in the proxy after the request processing, and you access is always proxy, this can solve the overload problem.
  8. Sometimes after visiting the Web server to access the file server, mainly to request some information in the database.
  9. The server will be packaged accordingly and returned to the client directly or through proxy (most of the time). The client decrypts the private key that was just generated and displays the content on the browser.

HTTPS encryption process details please go to HTTPS principle: Certificate delivery, validation and data encryption, decryption process resolution

4. In the middle of an app there is a button, after you touch the screen click, to the button to receive click events, what happened in the middle

There are several steps in the response chain

    1. The device packs the touch-Uitouch and Uievent objects into the event queue of the currently active application
    2. The uiapplication of the Singleton takes the touch event out of the event queue and passes it to the singleton UIWindow
    3. UIWindow using the Hittest:withevent: method to find the view in which the touch operation is located

Runloop, I'll tell you about this.

    1. The runloop of the main thread is awakened
    2. Notifies observer, handles timer and source 0
    3. Springboard to app process after receiving touch event
    4. Runloop processing of the source 1,source1 triggers a callback and calls _uiapplicationhandleeventqueue () for distribution within the app.
    5. Runloop processed to sleep before releasing the old autorelease pool and creating a new autorelease pool

Dig deeper to understand Runloop

Uiresponder is the parent class of UIView, and UIView is the parent of Uicontrol.

To declare, the 3rd question still has very big flaw, but because the deep digging place too many, this article cannot completely take into account, can only initiate. In addition, the purpose of the article is to interview the topic of learning, so write a bit more, may be in conflict with the interview skills and time.

Li Hongqiang iOS Classic face question

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.