Differences between Retain and Copy in IOS

Updated on technology 2024-02-08
4 answers
  1. Anonymous users2024-02-05

    copy : Create an object with an index count of 1 and then release the old object.

    retain: Releases the old object, assigns the value of the old object to the input object, and raises the index count of the input object to 1

    So what the hell does it mean?

    copy actually creates an identical object, while retain is not

    For example, an nsstring object with an address of 0 1111 and a content of @"str".

    After copying to another nsstring, the address is 0 2222, the content is the same, the new object retains 1, and the old object remains unchanged.

    After retaining to another nsstring, the address is the same (create a pointer, pointer copy), the content is of course the same, and the object has a retain value of +1

    That is, retain is a copy of the pointer, and copy is a copy of the content.

  2. Anonymous users2024-02-04

    retain, which is a shallow copy, does not change the object it points to, and the reference counter is added by 1 for MRC.

    copy, which is a deep copy, re-copies an object, the original object does not change, and the newly copied object, the reference counter is added by 1

  3. Anonymous users2024-02-03

    Reply,It's wrong.,Copy and mutablecopy are deep copies.,And copy can also be modified.,You can try to copy a string in the modification.,The result can be changed.,It doesn't affect the original string.,The original string is still there after the original string is released.,So the landlord is really a mistake!!

  4. Anonymous users2024-02-02

    Assign is used for simple data types, such as nsinteger, double, bool, retain and copy for objects, copy is used when A points to an object, B also wants to point to the same object, if you use assign, if A releases, and then call B will crash, if you use copy, A and B each have their own memory, you can solve this problem.

    retain will add one to the counter, which can also solve the problem of assign.

    Also: atomic and nonatomic are used to determine whether the getters and setters generated by the compiler are atomic operations. In a multi-threaded environment, atomic operations are necessary, otherwise they may cause erroneous results.

    With the addition of atomic, the setter function will look like this: if (property !).= newvalue)

Related questions
8 answers2024-02-08

Android is a Linux-based open-source operating system that is primarily used in portable devices. At present, there is no unified Chinese name, and more people in Chinese mainland use Android (unofficial) or Anzhi (official). The Android operating system was originally developed by Andy Rubin and initially mainly supported mobile phones. >>>More

11 answers2024-02-08

Personally, I think Android development will be better.

Because the restrictions of Android development are relatively low, and the development atmosphere is strong and the market is large. However, there are certain requirements for iOS development, which requires a US bank card (which is not clear to domestic developers), and an annual fee must be paid. >>>More

3 answers2024-02-08

The file class is used for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. You can also use the file class to get and set file properties or datetime information about file creation, access, and write operations. >>>More

31 answers2024-02-08

At this time, you should squeeze out some time to let go of what you are doing, go out for a walk, do something that makes you happy, and think about your goals. After calming down, think about what your problem is, if you don't know, you have to have the courage to communicate with the teacher, the teacher is very welcome to go, and they can also give you some advice.

5 answers2024-02-08

Torque is an important parameter of engine performance, which refers to the average torque output from the crankshaft end when the engine is running, commonly known as the "rotational force" of the engine. The greater the torque, the greater the "strength" of the engine output, the faster the crankshaft speed changes, and the better the car's climbing ability, starting speed and acceleration. Torque varies with the change of engine speed, too high or too low, the torque is not the maximum, only at a certain speed or in a certain speed range has the maximum torque, this range is the speed or speed range given when the maximum torque is marked. >>>More