Frequently asked questions and answers in Swift language

Source: Internet
Author: User

1, If I am a newly-started iOS developer, choose Swift study, or choose Objective-c study, or two all learn?

This can be decided according to two conditions: 1, I want to enter the company as an iOS development position 2, I just want to be an independent developer, their own development of the app Release.

In the first case, you have to learn objective-c, and now the vast majority of apps on the market are developed with oc, which is a long time transition from OC to swift.

Of course Swift should also learn at the same Time.

The second case: you can only learn swift if you do not consider compatibility (iOS7 below, which is mentioned later). But objective-c Third-party Open Source Library is very rich, there is time, it is best to learn about OC.

Over time, it must have turned to swift.

2, I have many years of iOS objective-c development experience, corresponding to swift, I am still a novice?

In my opinion, you are not a novice. Because you are familiar with the Xcode and Cocoa/cocoa Touch Apis. It takes longer to familiarize yourself with and learn about these APIs and tools than to learn swift.

Just take the time to familiarize yourself with the Swift syntax,

Swift can be quickly used in the Project.

3. IOS 8 and OS X Yosemite applications use only the Swift language?

No. Swift language can interact smoothly with objective-c, and Vice Versa. Apple has not completely turned the Objective-c API into swift, but you can still use these APIs in Swift Code. Time will prove that iOS and OS X stores will continue to rely on the Objective-c language for many years while adopting the Swift Language.

4. Does the swift language apply to other versions of iOS and OS X systems?


Yes! Xcode 6 can compile swift code for iOS 7 and above and OS X10.9 and Above. In fact, the WWDC app you've downloaded from the App Store is Apple in Swift Language.

however, Apple now does not allow applications created with the Xcode beta version to be submitted to the app Store. therefore, you will need to wait until the full version of Xcode 6 is released to upload the Swift-written application in the app Store.

5. Is swift language a replacement for objective-c or just a supplement to objective-c?


Quoting Apple's official word, "objective-c will not disappear, Swift and objective-c can be used for both cocoa and Cocoatouch Development. "so you can still continue to use OBJECTIVE-C. however, Apple seems to encourage you to use Swift for new development, rather than wanting you to rewrite all the objective-c code. We suspect that Apple will gradually reduce the use of objective-c language in the future framework and API development, or even discard the objective-c one day, so prepare for it!

6. What is playground?

Playground is just a file, you can write code and instantly see the effect of Running. It's really useful for learning Swift or new apis, prototyping code, or algorithms!

Refer to Article 11 and you will know how to create and run Playground.

7. How to learn swift?


Apple's Official Tutorial Swift programming Book

Chinese translation of official Tutorials: http://numbbbbb.gitbooks.io/-the-swift-programming-language-/



8. Is there anything that can be achieved in the swift language but cannot be achieved under the objective-c? or, conversely.

Some. Swift is a modern language that introduces a lot of content that objective-c does not support. such as namespaces (namspacing), optional types (optionals), tuples (tuples), generics (generics), type inference (types inference), and so On. Of course, Objective-c also has some features that Swift does not have, such as messaging Nil.

For more details, Please read the official documentation: Using Swift with Cocoa and Objective-c guide

You can also see here: Chinese version

9, playground, println () where is the result?

If you open the playground, or you can't see the result, do the following:

Open assistant Editor to see the console Output. Steps: View > Assistant editor > Show Assistant editor,

Or use the shortcut key: Option + Command + Return.

10, How to open playgrounds and see those cool values of the graphics?

Two ways to open playgrounds: create a new playgrounds project or create a new file in an existing project, select the playgrounds file.

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

For x in 1..10 {     x     

The following are the corresponding playgrounds

11. How do you run repl?


Run the following command at the terminal to tell it to use the command-line tool for Xcode 6.

sudo xcode-select-s/applications/xcode6-beta.app/contents/developer/
Then run the following code to start Swift REPL.

Xcrun Swift
If you are ready to exit, you can type: exit Or: quit. You can also use the Ctrl+d shortcut key.

12. Can you use Swift to invoke your own objective-c code or Third-party library? If so, how do you do it?


OK! When you add the First. Swift file to your Xcode project, you will be prompted to have Xcode create a bridge header file (bridging header files). You can import the Objective-c header file that you want to be visible in the swift code in this header File. then, All classes can be used for swift without importing, and you can use the same swift syntax as the system class to work with your custom objective-c Code.

13, arrays can be different types of packets?


In swift, it is strongly recommended to use strongly typed arrays that contain only one type, such as:

var  goodarray:string[] = ["foo",  "bar"]


technically, You can create an array that contains multiple types of Objects. But it's best to ask yourself why you're doing it. Multi-type arrays:

var  brokenarray:anyobject[] = ["foo", 1, 12.23,  true]

14. is the dictionary (dictionary) the same as it used to be? Are dictionaries also strongly typed (strongly typed)?


yes, but you can still use anyobject to solve it. For a dictionary, all the values in it are not the same type. The following is a dictionary representation of a JSON response returned from the server side:

Consider an example of a server JSON response, expressed in a dictionary:

var employee:dictionary<string, anyobject> = ["FirstName": "Larry", "LastName": "Rodgers", "Salary": 65_000.00]
This dictionary has two string-type keys and a double-value-type key. It's possible to have a dictionary directly, but You'd better create a class model object to represent the data, rather than relying on a dictionary.

15. How does Swift work with Grand Central dispatch?


In the same way, you can use the C API as you do in objective-c. when dealing with concurrency, you can also use the Apple Advanced Nsoperationqueue.

Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_background, 0), {  println ("test")  });  

16, Objective-c in the internationalization of the macro command it?


Similar to the nslocalizedstring in objective-c,

You can use nslocalizedstring (key:tableName:bundle:value:comment:) in swift. Ways to prepare for internationalization

。 tableName, bundle, and Value arguments all have default values, so if you're 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 need to consider! A retain cycle can still be created when two objects are strongly referenced to each other. You can break this retain cycle using the same method as in Objective-c. There are three keywords for declaring reference types, as detailed below, weak references and no master references will solve your reference life cycle Problems.

When do you use strong references, weak references, and no master references?

Strong References: strong references cause arc to retain instances until they are no longer needed. When all strong references are removed, the reference instance is Freed. Note by default, a strong reference is implicit, so you do not have to explicitly declare it.

Weak references: You should use weak references between objects in the independent life Cycle. When you set a weak reference for an object, you do not mind if the object is freed out of memory pressure. The value of a weak reference must be a variable, defined using var, and must be used? The optional type of the Operator. Because a weak reference is optional, you must not end up with a reference to an invalid instance that does not already exist. When the reference instance is freed, arc automatically sets the reference to Nil.

No main reference: You should use a non-primary reference for objects that have the same life cycle, such as when an object points to itself, and you want to avoid a retain cycle. No primary reference can be used whenever a reference has a value, but when you need to tell arc not to set it to Nil. A non-primary reference behaves like a objective-c unsafe_unretained. You want to make sure that you don't access the reference after the Reference object is freed, which can cause your app to Crash. A no-primary reference cannot be optional and cannot be set to Nil. A non-primary reference is also implicit parsing.

18. How to use semicolons


Semicolons are optional in swift, but for legibility purposes, Apple recommends that you stop using semicolons. however, sometimes semicolons are still used in swift, such as in a looping statement.

19. What will be the next development of swift?

Now Swift is only the first version, Apple's purpose is very clear, they will iterate to update the Language. So you find Swift's bug, you can report it to apple, or ask for some new features to be Added.

There is still a lot of room for improvement before the release is officially Released.

Frequently asked questions and answers in Swift language

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.