Summarize 20 common questions and answers in the swift Language

Source: Internet
Author: User

1. if I am an entry-level IOS developer, should I select swift for learning, objective-C for learning, or both?

This can be decided based on two situations: 1. I want to enter the company's position for iOS development 2. I just want to be an independent developer and develop an app for publishing.

In the first case, you must learn objective-C. Currently, most apps on the market are developed with OC. The transition from OC to swift takes a long time.

Of course, SWIFT should also learn at the same time.

Case 2: If you do not consider compatibility (iOS 7 or earlier versions will be mentioned later), you can simply learn Swift. However, objective-C third-party open source libraries are rich. If you have time, you 'd better learn about oC.

Over time, it must have switched to SWIFT.


2. I have many years of IOS objective-C development experience. Are I new to Swift?

In my opinion, you are not a newbie. Because you are familiar with xcode and Cocoa/cocoa touch APIs. It takes longer to familiarize yourself with and learn these APIs and tools than to learn Swift.

Take some time to familiarize yourself with the swift syntax,

You can use Swift quickly in the project.


3. Do iOS 8 and OS X Yosemite applications only use the swift language? No. Swift can interact smoothly with objective-C, and vice versa. Apple has not completely converted objective-C APIs into swift, but you can still use these APIs in Swift code. Time will prove that the iOS and OS X stores will continue to rely on objective-C language while adopting the swift language in many years.


4. is swift applicable to other IOS versions and OS X systems?
Yes! Xcode 6 can compile Swift code for iOS 7 and later versions and OS x10.9 and later versions. In fact, the WWDC app you downloaded from the App Store is written by Apple in swift.

However, Apple does not allow applications created using xcode Beta to be submitted to the app store. Therefore, you must wait until the official version of xcode 6 is released to upload the swift application to the app store.


5. Does the swift language replace objective-C, or is it just a supplement to objective-C?
By referencing Apple's official statement, "objective-C will not disappear. Swift and objective-C can be used for both cocoa and cococoatouch development ." Therefore, you can continue to use objective-C. However, apple seems to encourage you to use swift for new development, rather than wanting you to rewrite all objective-C code. We guess Apple will gradually reduce the use of objective-C language in the future framework and API development, or even discard objective-C one day, so be prepared early!


6. What is playground?
Playground is just a file. You can write code and immediately see the running effect. It is really useful for learning swift or new APIs, prototype code, or algorithms!

Refer to 11 articles to learn how to create and run playground.


7. How to Learn Swift?
Official Apple tutorial swift programming book

Http://numbbbbb.gitbooks.io/-the-swift-programming-language -/ ()

This is the fastest translation tutorial I have ever seen! Translation is fast and good.

Swift video of WWDC 2014

8. Is there anything that can be implemented in swift but not in objective-C? Or, in other words. Yes. SWIFT is a modern language that introduces a lot of content not supported by objective-C. For example, namespace (namspacing), optional type (optionals), tuples, generic, type inference, and others. Of course, objective-C also has some features not available in swift, such as messaging nil.


For more details, read Apple's official document: using SWIFT with cocoa and objective-C guide)


9. Is there any API that SWIFT cannot use? I have not found this article.


10. Where is the result of println () in playground? If you open playground and still cannot see the result, perform the following operations:

Open the assistant editor to view the console output. Step: View> assistant editor> show assistant editor,

Alternatively, use the shortcut key: Option + command + return.


11. How can I open playgrounds and view those graphics with cool values?

You can open playgrounds in either of the following ways: Create a playgrounds project or create a file in an existing project, and select the playgrounds file.

Let's look at a simple code, a for loop,

for x in 1..10 { x }
The following is the corresponding playgrounds


12. How do you run repl?
Run the following command on the terminal to tell it to use the command line tool of xcode 6.

Sudo xcode-select-S/apps/Xcode6-Beta.app/contents/developer/
Run the following code to start swift REPL.

Xcrun swift
If you are about to exit, enter exit or quit. You can also use the CTRL + D shortcut.

12. Can you use swift to call your own objective-C code or third-party libraries? If yes, what should I do?
Yes! When you add the first. Swift file to the xcode project, the system will prompt you to ask xcode to create a bridge header file (bridging header file ). You can import the objective-C header file that you want to see in Swift code in this header file. Then, all classes can be used by swift without being imported. You can use the same swift syntax as the system class to use custom objective-C code.


13. Can arrays be of different types?
In swift, it is strongly recommended to use a strongly-typed array containing only one type, for example:
VaR goodarray: String [] = ["foo", "bar"]
Technically, you can create an array containing multiple types of objects. But you 'd better ask yourself why. Multi-type array:
VaR brokenarray: anyobject [] = ["foo", 1, 12.23, true]


14. Is the dictionary the same as the original one? Is the dictionary strongly typed?
Yes, but you can still use anyobject. For a dictionary, it also makes sense that all values in it are not of the same type. The following is a JSON response returned from the server in the dictionary:

Let's look at an example of the server's JSON response, which is represented in a dictionary:

VaR EMPLOYEE: dictionary <string, anyobject> = ["firstname": "Larry", "lastname": "Rodgers", "salary": 65_000.00]
This dictionary has two string-type keys and one double-type key. It is also possible to directly use a dictionary, but you 'd better create a level-1 model object to represent the data, rather than relying on the dictionary.


15. How does swift work with Grand Central Dispatch?
In the same way, you can use the c api as in objective-C. You can also use Apple's advanced nsoperationqueue when dealing with concurrency.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {println( "test" )});


16. What about the internationalized macro commands in objective-C?
Similar to nslocalizedstring in objective-C,

You can use nslocalizedstring (key: tablename: Bundle: Value: Comment :) In swift to prepare for internationalization.

. Tablename, bundle, and value arguments all have default values. Therefore, if you are using nslocalizedstring, you can write the following code:

NSLocalizedString("Hello", comment: "standard greeting")

17. Do I need to consider circular references for Swift?
Of course you need to consider it! When two objects are strongly referenced to each other, a retain cycle can still be created. You can use the same method in objective-C to break the retain cycle. There are three keywords used to declare the reference type. The details are as follows. Weak references and no primary references will solve your reference lifecycle problem.

When can I use strong references, weak references, and no primary references?

Strong references: strong references will make the arc reserve instances until they are no longer needed. When all strong references are removed, the referenced instance is released. Note that strong references are implicit by default, so you do not need to explicitly declare them.

Weak references: You should use weak references between objects with independent lifecycles. When you set a weak reference for an object, if the object is released due to memory pressure, you don't mind this. The weak reference value must be a variable defined by VAR and must be used? Optional type of the operator. Weak references are optional, so you cannot end with a reference to an invalid instance that does not exist. When the referenced instance is released, arc automatically sets the reference to nil.

No primary reference: You should use no primary reference for objects with the same lifecycle. For example, when an object points to itself, and you want to avoid a retain cycle. No primary reference can be used whenever there is a reference value, but when you need to tell arc not to set it to nil. The act of no primary reference is similar to the unsafe_unretained of objective-C. Make sure that you do not access the reference after the referenced object is released, which will cause your app to crash. No primary reference is optional and cannot be set to nil. No primary reference is also implicitly parsed.
 

18. How to Use semicolons
Semicolons are optional in swift, but for the sake of ease of use, Apple recommends that you do not use semicolons any more. However, you may still use semicolons in swift, for example, in loop statements.

for var index = 0; index < 3; ++index { ... }

19. How will swift develop?

Currently, SWIFT is only the first version. Apple has a very clear purpose and they will update the language iteratively. Therefore, you can report swift bugs to Apple or ask for new features.

There is still much room for improvement before the official release of this version.


20. How does cocoapods apply to swift projects?
The SWIFT project is still an xcode project and supports multiple targets, but it has potential space to Improve the Ability of creating modules and custom frameworks. Cocoapods may be rewritten to adapt to this new feature.

Someone is using cocoapods to assist SWIFT project work: the issue is already being discussed: here: https://github.com/CocoaPods/CocoaPods/issues/2218.


Note:

This article is mainly reference: http://www.raywenderlich.com/74138/swift-language-faq

However, I will not translate words by words. I have added my own ideas based on the reference of the original article, and briefly wrote down the questions and answers.


Related Article

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.