Isa pointer:
In Objective-C, the definition of any class is an object. There is no essential difference between a class and an instance (object) of a class. Any object has an ISA pointer.
Isa isa pointer to Class type. Each instance object has a pointer to ISA, which points to the object’s Class, and the Class has a pointer to Isa, which points to a meteClass. Metaclasses hold a list of class methods. When a class method is called, the metaclass looks for its implementation from itself. If not, the metaclass looks for the method from its parent. Also note that metaclasses are classes, and they are objects. The metaclass also has isa Pointers, and its ISA pointer ends up pointing to a root metaclass. The isa pointer to the root metaclass points to itself, forming a closed inner loop.
Meaning const
-
The interview questions
const int a; int const a; const int *a; int const *a; int * const a; int const * const a; Copy the code
- The first two do the same thing: A is a constant integer
- The third and fourth values mean that a is a pointer to a constant integer. (Integers are immutable, but Pointers can be.)
- A is a constant pointer to an integer.
- The last one means that a is a constant pointer to a constant integer (a pointer to an integer is immutable, and a pointer is immutable)
-
Proper use of the const keyword enables the compiler to protect undesired parameters from unintentional code modification and reduce bugs.
- To prevent a variable from being changed, use the const keyword. When defining this const variable, we usually need to initialize it because there is no chance to change it later;
- For Pointers, we can specify that the pointer itself is const, that the data to which it points is const, or that both are const.
- In a function declaration, const can modify the parameter to indicate that it is an input parameter and cannot change its value inside the function;
- If a member function of a class is const, it is a constant function and cannot modify a member variable of the class.
- For member functions of a class, it is sometimes necessary to specify that the return value is const so that the return value is not an “lvalue.”
The static keyword
- Function in the body
static
The scope of a variable is the function body, unlikeauto
A variable whose memory is allocated only once, so its value remains the same the next time it is called; - Inside the module
static
Global variables can be accessed by functions used in the module, but not by other functions outside the module. - Inside the module
static
A function can only be called by other functions in the module. The use of this function is restricted to the module in which it is declared. - In the class
static
Member variables are owned by the entire class, are initialized only once, and are reclaimed only when the program exits. - In the class
static
A member function is owned by the entire class and is not accepted by this functionthis
Pointer, and thus can only access classstatic
Member variables.
static
use- Limits the scope of variables
- Sets the storage domain for the variable
The volatile keyword
Volatile variables mean that they can be changed unexpectedly, so that the compiler does not make assumptions about their value. To be precise, the optimizer must carefully re-read the value of the variable each time it is used, rather than using a backup stored in a register. The following is
- A few examples of volatile variables
- Hardware registers of parallel devices (e.g., status registers)
- Non-automatic variables that will be accessed in an interrupt service subroutine
- A variable shared by several tasks in a multithreaded application
- Can a parameter be both const and volatile? Explain why. Yes. One example is a read-only status register. It is volatile because it can be changed unexpectedly. It’s const because the program shouldn’t try to modify it
- Can a pointer be volatile? Explain why. Yes. Although it’s not very common. An example is when a service subroutine fixes a pointer to a buffer.
Self. Is different from self->?
Self is a call to the get or set method. Self is the current object itself, which is a pointer to the current object. Self -> is a direct access member variable
Lazy loading
Lazy loading, also known as lazy loading, is initialized only when it is used. For example, the controller view is created when it is used for the first time by calling the loadView method. Lazy loading refers to its GET method. I think one of the best and easiest examples is the loading display of an image in a tableView. A delayed load to avoid high memory, and an asynchronous load to avoid thread clogging. Note: If the load is lazy, be sure to check whether it already exists and instantiate it if not
- Benefits of lazy loading:
- You don’t have to write all the code to create the object in the viewDidLoad method, so the code is much more readable
- Getter method of each control is responsible for their own instantiation processing, the code between the independence of strong, loose coupling
What’s a selector in Objective C?
So you can see that @selector() is taking the number of a class method, and it basically behaves like a pointer to a function in C, except in C, you can assign a function name to a function pointer, whereas in Objective-C classes you can’t apply a function pointer directly, so you just have to do an @selector syntax to get that. The result is an SEL type. This type is essentially the number (function address) of the class method. How are methods different from selectors? A: Selector is the name of a method, and method is a combination that contains the name and implementation. A selector finds the method address and calls a method
other
-
[[NSMutableArray alloc]init] and [NSMutableArray array]
[[NSMutableArray alloc]init]
alloc
Allocate memory,init
Initialization, need to manually release;[NSMutableArray array]
You don’t have to do it manuallyrelease
, follow theautoreleasepool
mechanism- In ARC (Automatic reference Counting) there is no difference between the two methods, right
-
New and alloc/init are basically the same now, except that new can only be initialized by default init, while alloc can be initialized by other init methods.
-
The child thread must actively start runloop
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{[self performSelector:@selector(fireBlock: withObject:^{NSLog(@" Hello world");} afterDelay:0.3]; });Copy the code
The code snippet above was originally intended to print Hello World after an asynchronous delay of 0.3 seconds. But when it runs, it doesn’t print Hello World. The reason is: NSRunLoop for non-main thread is not enabled by default, – (void)performSelector (SEL)aSelector withObject (nullable ID)anArgument afterDelay (NSTimeInterval)delay; The function is internally implemented by NSTimer timer. If NSRunLoop is not enabled, NSTimer will not run normally.
-
Class loading and initialization
+ (void)load;
Call: all classes are loaded into memory as soon as the program starts+ (void)initialize;
Call: When this class or subclass is used for the first time, call function: initializes the class
-
Code layout or storyboard? For complex and dynamically generated interfaces, you are advised to write interfaces manually. For buttons or UI controls that require a uniform style, manual code construction is recommended. Easy to modify and reuse later. For UIView classes or UIViewController classes that require inheritance or composition relationships, it is recommended to write interfaces manually in code. For simple, static, non-core functional interfaces, consider xiB or Storyboard.
-
Some pictures load slowly how to deal with? How did you optimize the performance of the program?
- Images are downloaded on an asynchronous thread
- The image download process uses placeholder images
- If the image is large, consider multithreaded breakpoint download
-
What are the differences between Foundation objects and Core Foundation objects
- Foundation objects are OC, Core Foundation objects are C objects
- Conversion between data types ARC:
__bridge_retained
、__bridge_transfer
The ARC:__bridge
-
Write a “standard” macro that outputs two parameters and returns a larger #define MIN(X,Y) ((X)>(Y)? (Y):(X))
-
Is there garbage collection in iPhone OS? There is no
-
What is the difference between self.name= “object” and name= “object”
-
Are there private methods in Objective-C? What about private variables? There’s no private method, but you can implement a method directly in a.m file that’s not declared in a.h file, and it’s not externally accessible. There are private variables
-
Is there multiple inheritance in Objective-C? If not, is there a statement instead? Without multiple inheritance, multiple inheritance can be simulated by protocol
-
The ability of different objects to respond to the same message in their own way is called polymorphism. It is assumed that all life classes use the same method — eat; Then, human beings and pigs are also creatures. After inheriting life, they both realize their own EAT. However, we only need to call their own EAT methods. Polymorphism. Basically, the determination of data types is deferred from compile time to run time. Such as changing the implementation of a method at runtime, or enumerating what properties an object has, and so on. That is, different objects respond to the same message in their own way (in response to the eat selector). Thus it can be said that the runtime mechanism is the foundation of polymorphism. Polymorphic, subclass Pointers can be assigned to the parent class. It is generally best to have a self-aware understanding of polymorphism, inheritance and encapsulation. From the perspective of programming, it means to view objects, classes, methods and so on in OC from the perspective of C language, that is, classes, objects, methods are mapped to some structures, functions, Pointers, etc. For example, if you want to enumerate what properties an object has, you can call some functions in the operation. JSonModel and YYModel are implemented based on the runtime. By enumerating which attributes and types of attributes in the model, it goes to the corresponding JSON (array and dictionary) to find the corresponding fields, and then completes the analysis
-
Advantages and disadvantages of OBJ-C
- advantages
- Cateogies
- Posing
- Runtime characteristics
- Index calculation
- Elastic messaging
- Not an overly complex C derivative language
- Objective-c and C++ can be mixed programming
- Disadvantages:
- Namespaces are not supported. For naming conflicts, you can use a long naming method or a special prefix. For naming conflicts between imported third-party libraries, you can run the link command and flag command to resolve the conflicts
- Operator overloading is not supported
- Multiple inheritance is not supported
- With dynamic runtime types, all methods are function calls, so many compile-time optimization methods are not used. (such as inline functions), poor performance.
- advantages
-
Static language: The source code you write is compiled completely into machine code. Dynamic language: the source code you write is not compiled completely into machine code
-
Is there a difference in memory between global and local variables? If so, what is the difference? Global variables are stored in a static database and local variables are stored on the stack
-
What are the properties of variables declared by ID?
- There is no *
- Dynamic data type
- The variable declared by id can point to any OC object (set to nil) regardless of its type
- Check its specific type at run time
- Any (existing) message can be sent to it
-
Apple’s security regime:
- You can’t access user information without their consent.
- All programs run in the sandbox, and program B cannot enter program A’s execution scope.
- If it is related to money, such as Alipay, the underlying implementation is confidential and only provides interfaces for developers to call, so that security is guaranteed.
- If you want to prevent your code from being decompiled, you can package the.m files in your own code as static libraries (.a files) or framework files and only make them available to others. This ensures the security of your personal code.
- For web logins, you send a POST request related to the username and password. For GET requests, the password is displayed directly in the URL. Our company uses MD5, but now there is a special website to crack MD5. In order to prevent this, we can use salt technology.
-
Write an implementation of NSString class
+ (id)stringWithCString: (c*****t char*)nullTerminatedCString encoding: (NSStringEncoding)encoding { NSString *obj; obj = [self allocWithZone: NSDefaultMallocZone()]; obj = [obj initWithCString: nullTerminatedCString encoding: encoding]; return AUTORELEASE(obj); } Copy the code
-
What is a balanced binary tree? Both the left and right subtrees are balanced binary trees and the absolute value of the depth difference between the left and right subtrees is not greater than 1
-
Can local variables have the same name as global variables? Yes, the local will block the global. To use a global variable, you need to use “::” local variables can have the same name as the global variable. When referencing this variable within a function, the local variable with the same name is used, but the global variable is not used. For some compilers, it is possible to define more than one local variable with the same name in the same function, such as defining a local variable with the same name in both bodies, and the scope of that local variable is in that body
-
How to reference a defined global variable? A: Can be used to reference a header file, you can also use the extern keyword, if use reference header files to a reference in global variables declared in a header file, assuming that you write that change is wrong, then an error during compilation, if you use the way of extern reference, assume that you made the same mistake, so won’t an error at compile time, An error was reported during connection.
-
What’s the difference between NSString and NSMutableString? NSString is a const char* that cannot be changed. NSMutableString is the equivalent of char* that can change the contents inside.
-
Image manipulation: imageNamed: The advantage is that images are cached when loaded. Images are good to use frequently, usually for loading small images. IamgeWithContentsFile: Large image. Every time I call it, it takes up the cache. ImageWithContentsOfFile: Only images are loaded, image data is not cached. So for larger images and less use, you can use this method to reduce memory consumption.
-
What are the implications of @protected, @private, and @public declarations for class instance (member) variables? @protected: Protected. This instance variable can only be accessed within the class and its subclasses, not within other classes. @private: private. This instance variable can only be accessed within the class. @public: public. This instance variable is accessible to everyone.
-
@dynamic: tells the compiler that the methods for getting and assigning attributes are implemented by the user, not automatically generated. You need to provide setters for read-only properties and setters and getters for read-write properties. @synthesize: means that the compiler generates getter and setter property declarations unless the developer has already done it.
-
What design pattern does UIscrollVew reach? Can you find anything like this in the Foundation library? Composition: All container Views use this pattern observer: All UIresponders use this pattern. Template: All datasource and Delegate interfaces are typical applications of the Template pattern
-
Y = 10 Cause: OC syntax does not allow you to directly modify the members of a structure attribute of an object. _btn is an object, frame is a structure. Object and structure are not the same, structure is in C language, which can define many attributes, but can not define methods, and object is that you can define attributes and methods, is a typical object-oriented syntax. How to change a member of a structure property in an object:
CGRect frame = _btn.frame; Y -= 10; _btn.frame = frame;Copy the code
or
Y = _btn.frame.origin. Y; // Change y -= 10; // reset _btn y value, Frame = CGRectMake(_btn.frame.origin. X, y, _btn.frame.size. Width, _btn.frame.size. Height);Copy the code
-
If you want the same control to change position at the same time, and zoom in. This setting has no effect. This will create a new transform and then assign it, and the second transform to the button will overwrite the previous one, so it won’t have the desired effect.
_btn.transform = CGAffineTransformMakeTranslation(0, 100); _btn. Transform = CGAffineTransformMakeScale (1.2, 1.2);Copy the code
Solutions:
_btn.transform = CGAffineTransformMakeTranslation(0, 100); // In the case of the previous transform, continue to add the scaled deformation. _btn.transform = CGAffineTransformScale(_btn.transform, 1.2, 1.2);Copy the code
-
Rounding problems
Float I = 1.7; NSLog(@"%0. F ", I); Int j = (int) I; int j = (int) I; NSLog(@"%d",j); // Prints result 1Copy the code
-
What does id and nil stand for?
- Id and void star are not exactly the same. In the above code, ID is a pointer to struct objc_Object, which basically means that ID is a pointer to any Object that inherits the Object (or NSObject) class. Note that ID is a pointer, so you don’t need an asterisk when using id. For example, id foo=nil defines a nil pointer, which points to any subclass of NSObject. And id *foo=nil defines a pointer, and that pointer points to another pointer, and that pointer points to a subclass of NSObject.
- Nil is the same as NULL in C and is defined in objc/objc.h. Nil represents an objtive -c object whose pointer points to null (nothing is null). Nil with a capital letter is a little different from Nil in that Nil defines a pointer to an empty Class (Class, not object).
-
What are the common object-C data types, and how do they differ from the basic c data types? Such as NSInteger and int answer: The data types of object-C include NSString, NSNumber, NSArray, NSMutableArray, NSData, etc. These are all classes and are objects after creation. The basic data type of C language, int, is only a certain amount of memory space for storing values. NSInteger is a basic data type, not a subclass of NSNumber, and certainly not a subclass of NSObject. NSInteger is an alias for the basic data type Int or Long (the definition of NSInteger typedef Long NSInteger). The difference is that NSInteger determines whether it is itself an Int or a Long depending on whether the system is 32-bit or 64-bit.