Simple proofreading of Swift and Objective-C

Recent Trivia

Recently, some problems caused by the migration of Swift version have attracted my attention!
in xCode 8.3. 2, has not been able to compile Swift2.3 again, so all of the swift2.3 projects have to migrate to 3.1.
: I’ve written another project on swift3.0 before, but it’s still a lot of trouble to change it.
admittedly, the development speed of swift will be much faster than the traditional Objective-C, after all, simplify the content, optimize the performance.
this time, I also need to use Objective-C to rewrite a project written in swift, so a few days down, the project did not finish, but wrote this article complained.

In a simple assignment modification problem, I used the following method to organize a wave of data on Objective-C:

NSDictionary *dict1 = "title" @{@: @ 123456, @ @ "image": "aux_task"}; [_collectionArray addObject:dict1];

And then when I need to add or modify it later:

_collectionArray[0][@ "image" = @ @ light_task_selected";

There will be the following

-[__NSCFNumber setObject:forKeyedSubscript:]: unrecognized selector sent to instance 0xb000000000000002 2017-04-24 16:31:30.589128+0800 BleMate2[1603:771672] app due to uncaught exception * * * Terminating'NSInvalidArgumentException', reason: __NSCFNumber setObject:forKeyedSubscript:] unrecognized selector sent'-[: to instance 0xb000000000000002'

???
? WTF?
! The reason is that Objective-C is a reference type in the grammar, to Array inside the input when the dict object, is its attributes of the whole object is also included, that is to say, if it is not allowed to modify attributes in them, or do not modify!
can be written in such a way that you can continue to modify:

NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setValue:@ "123456" forKey:@ "title"]; [dict setValue:@ "bt_task" forKey:@ "image"]; [_collectionArray addObject:dict]; _collectionArray[0][@ "image"] = "@ light_task_selected";

The swift is simple for these, it uses a copy of the structure of the way, that is, when you throw in the content:

/ / the first set of a structure struct AboutItemInfo titleName:String var image:UIImage {var} / / we then define a value, and assign the let aboutArray:[AboutItemInfo]! Let item = [] = AboutItemInfo.init ("titleName: nihao", image:#imageLiteral (resourceName: heart_rate)) aboutArray.append (item), / / then if you want to modify the content. Only need to directly read out modifications to aboutArray[0].titleName = nihuai"

Why is this possible?! Because
is the run mode of replication mode, so the aboutArray inside the object, is already in the process of pass is copied to the AboutArray inside, when read again modified, is to modify the value of AboutArray.
I think it’s possible that Objective-C uses other methods to replace the same way as above. So I turned over the ancient Objective-C structure

Typedef struct NSString *name; NSString *Author; int book_id;} book Book{;

But the word on to the xCode found it prompts an error: ARC forbids Objective-C objects in Struct
suddenly realized in automatic release, these things would not be reliable, because if not released will cause memory overflow, so to solve the above problem, can turn it into a manual release after trying. Finally succeeded! (but feel like going back for more than ten years. Although the official
…) are also given for this type of structure on the ARC after the body to the best class to solve, but compared to the Swift light, also appears to be too bloated. Then carefully thought, why Swift in the major evaluation platform is always faster than Objective-C run?!

Chinese Leopard

On the current problems, can be classified as the war between the structure and the class!
in swift, including many of the basic types, such as String, Array and Dictionary types are implemented in the form of structures, which means that is assigned to a new constant or variable, or in these basic types are passed to a function or method, their value will be copy.
in Objective-C, NSString, NSArray, NSDictionary types are as in the form of, but not the structure, when they are assigned, passed functions or methods, they will not be copied, but the reference is passed through an existing instance.
specific reference to the following example:

/ / in Objective-C, use the following method for international multi language of _headTitleLab.text = NSLocalizedString ("BLEConnect_Ble", "@ @"); / / when I changed the language setting to change the [NSBundle setLanguage: "en"] application; then _headTitleLab will show you / / into another language, do not need to re assignment.

So if you want to do the same operation in the swift you need to re evaluate the value of its operation, or it will not refresh the language.

/ in Swift, use the following method for international multi language of headTitleLab.text = NSLocalizedString ("BLEConnect_Ble", "comment:") / / when there is a language setting to change the Bundle.setLanguage application ("en") / / if you want to apply the language with the change, it will be necessary to be successful (headTitleLab.text = NSLocalizedString assignment "BLEConnect_Ble", "comment:")

It is also worth mentioning that:
is a string, array, dictionary of the “copy” behavior description. In your code, copy behavior always seems to happen. However, Swift does not perform actual copies only when absolutely necessary. Swift manages all value copies to ensure performance optimization, so you don’t need to avoid assigning to optimize performance.
and Objective-C approach is to first instantiate the class, and then do data storage processing, so here is also greatly reduced performance.
let me feel that Swift is actually a structural language, and Objective-C is a reference to the class language, and now want to structure a simple assignment is indeed conducive to the speed of operation.
this is how to closure in-depth study of swift than Objective-C Block, in-depth study, but the closure of the understanding of swift is deficient, have in-depth understanding of a content can only be written.

Write in the end

Today, Mr. Wang also see the article, it should be understood as the development of science and technology and means of continuous improvement and optimization, but to solve the problem, or to proceed from reality. After all, we exist to solve problems. Objective-C and Swift on the same, in fact, when the Objective-C stable for so many years of big brother, there is still a certain merit.

In addition, on the swift and Objective-C in the end what language should be used for development, this kind of thing is really bad to say! If there is a choice, I still choose Swift bar! Of course, in order to develop the process of mature team can let Swift fly.

Increasingly, it is important to note that the warnings that xCode has made during development, because you do not know when it is suddenly not compatible!


In this paper, reference:

Wang _ mentor school geeks