Swift

Swift is a general-purpose, open-source programming language developed by Apple Inc. for their platforms and Linux. Use the tag only for questions about language features, or requiring code in Swift. Use the tags [ios], [osx], [watch-os], [tvos], [cocoa-touch], and [cocoa] for (language-agnostic) questions about the platforms or frameworks.

55 votes
10 answers
1.2K views
Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?

Why would you create a "Implicitly Unwrapped Optional" vs creating just a regular variable or constant? If you

avatar
Lawanda
0 1 0 9
asked 07 September, 2021
64 votes
6 answers
3K views
Swift: print() vs println() vs NSLog()

What's the difference between print, NSLog and println and when should I use each? For example, in Pytho

avatar
Stacia
0 1 0 9
asked 07 September, 2021
49 votes
19 answers
1K views
How to add constraints programmatically using Swift

I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some

avatar
Corrin
0 1 0 9
asked 07 September, 2021
54 votes
22 answers
1.6K views
Any way to replace characters on Swift String?

I am looking for a way to replace characters in a Swift String. Example: "This is my string" I would like to

avatar
Freida
0 1 0 9
asked 07 September, 2021
52 votes
17 answers
3K views
Why Choose Struct Over Class?

Playing around with Swift, coming from a Java background, why would you want to choose a Struct instead of a C

avatar
Juliane
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
3.1K views
How would I create a UIAlertView in Swift?

I have been working to create a UIAlertView in Swift, but for some reason I can't get the statement right

avatar
Nidia
0 1 0 9
asked 07 September, 2021
62 votes
17 answers
972 views
How to check if an element is in an array

In Swift, how can I check if an element exists in an array? Xcode does not have any suggestions for contain, i

avatar
Vi
0 1 0 9
asked 07 September, 2021
59 votes
23 answers
3.1K views
What does an exclamation mark mean in the Swift language?

The Swift Programming Language guide has the following example: class Person { let name: String init

avatar
Keila
0 1 0 9
asked 07 September, 2021
55 votes
6 answers
1.3K views
Why are emoji characters like ?‍?‍?‍? treated so strangely in Swift strings?

The character ?‍?‍?‍? (family with two women, one girl, and one boy) is encoded as such: U+1F469 WOMAN,

avatar
Joseph
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
2K views
How to enumerate an enum with String type?

enum Suit: String { case spades = "♠" case hearts = "♥" case diamonds = "♦" case clubs =

avatar
Dorian
0 1 0 9
asked 07 September, 2021
56 votes
27 answers
2.7K views
How do I check if a string contains another string in Swift?

In Objective-C the code to check for a substring in an NSString is: NSString *string = @"hello Swift"; NSRan

avatar
Alphonso
0 1 0 9
asked 07 September, 2021
53 votes
18 answers
2.6K views
Swift how to sort array of custom objects by property value

lets say we have a custom class named imageFile and this class contains two properties. class imageFile {

avatar
Wilber
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
1K views
UIScrollView Scrollable Content Size Ambiguity

Fellow devs, I am having trouble with AutoLayout in Interface Builder (Xcode 5 / iOS 7). It's very basic

avatar
Stacia
0 1 0 9
asked 07 September, 2021
56 votes
26 answers
2.8K views
dispatch_after - GCD in Swift?

I've gone through the iBook from Apple, and couldn't find any definition of it: Can someone explain

avatar
Emilia
0 1 0 9
asked 07 September, 2021
53 votes
7 answers
1.9K views
How can I make a weak protocol reference in 'pure' Swift (without @objc)

weak references don't seem to work in Swift unless a protocol is declared as @objc, which I don't wa

avatar
Venice
0 1 0 9
asked 07 September, 2021
53 votes
30 answers
1.8K views
Using a dispatch_once singleton model in Swift

I'm trying to work out an appropriate singleton model for usage in Swift. So far, I've been able to

avatar
Clarice
0 1 0 9
asked 07 September, 2021
63 votes
23 answers
436 views
Convert Int to String in Swift

I'm trying to work out how to cast an Int into a String in Swift. I figure out a workaround, using NSNum

avatar
Deedee
0 1 0 9
asked 07 September, 2021
62 votes
22 answers
2.2K views
UITextField text change event

How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for

avatar
Freida
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
1.3K views
iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

This crash has been a blocking issue I used the following steps to reproduce the issue: Create a Cocoa Touch

avatar
Solomon
0 1 0 9
asked 07 September, 2021
58 votes
19 answers
1.4K views
Do Swift-based applications work on OS X 10.9/iOS 7 and lower?

Will Swift-based applications work on OS X 10.9 (Mavericks)/iOS 7 and lower? For example, I have a machine ru

avatar
Keith
0 1 0 9
asked 07 September, 2021