Questions tagged [swift]
Swift is a general-purpose programming language developed by Apple Inc first released in 2014 for its platforms and Linux. Swift is open-source. Use the tag only for questions about language features or requiring code in Swift. Use the tags [ios], [ipados], [macos], [watch-os], [tvos], [swiftui], [cocoa-touch], and [cocoa] for (language-agnostic) questions about the platforms or frameworks.
swift
33,252
questions
1500
votes
44
answers
525k
views
Passing data between view controllers
I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following:
I have a view that acts as a data entry form and I want to give the user the option to select multiple ...
506
votes
21
answers
651k
views
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
My Swift program is crashing with EXC_BAD_INSTRUCTION and one of the following similar errors. What does this error mean, and how do I fix it?
Fatal error: Unexpectedly found nil while unwrapping ...
113
votes
13
answers
91k
views
Returning data from async call in Swift function
I have created a utility class in my Swift project that handles all the REST requests and responses. I have built a simple REST API so I can test my code. I have created a class method that needs to ...
156
votes
3
answers
29k
views
Protocol doesn't conform to itself?
Why doesn't this Swift code compile?
protocol P { }
struct S: P { }
let arr:[P] = [ S() ]
extension Array where Element : P {
func test<T>() -> [T] {
return []
}
}
let ...
112
votes
1
answer
31k
views
Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly
I have an array of keys which lead to post objects for my social network like so /posts/id/(post info)
When I load the posts I load /posts/0 and then /posts/1 etc using the observeSingleEventOfType(....
1138
votes
17
answers
380k
views
How do I call Objective-C code from Swift?
In Swift, how does one call Objective-C code?
Apple mentioned that they could co-exist in one application, but does this mean that one could technically re-use old classes made in Objective-C whilst ...
522
votes
39
answers
761k
views
Loading/Downloading image from URL on Swift
I'd like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I've a compilation error:
'imageWithData' is unavailable: use object ...
80
votes
4
answers
14k
views
How to initialize properties that depend on each other
I want a picture to move to the bottom. If I press a button the pic should move down by 1.
I added the picture and a button:
var corX = 0
var corY = 0
var runter: UIButton = UIButton....
584
votes
26
answers
296k
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 the structure of dispatch_after?
dispatch_after(<#when: dispatch_time_t#>, <#queue: ...
329
votes
25
answers
103k
views
How do I shuffle an array in Swift?
.shuffle() and .shuffled() are part of Swift
Original historic question:
How do I randomize or shuffle the elements within an array in Swift? For example, if my array consists of 52 playing cards, I ...
393
votes
21
answers
367k
views
Swift - encode URL
If I encode a string like this:
var escapedString = originalString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
it doesn't escape the slashes /.
I've searched and found this ...
253
votes
14
answers
181k
views
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
How to generate a date time stamp, using the format standards for ISO 8601 and RFC 3339?
The goal is a string that looks like this:
"2015-01-01T00:00:00.000Z"
Format:
year, month, day, as &...
124
votes
11
answers
139k
views
Correctly Parsing JSON in Swift 3
I'm trying to fetch a JSON response and store the results in a variable. I've had versions of this code work in previous releases of Swift, until the GM version of Xcode 8 was released. I had a look ...
596
votes
30
answers
139k
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 get a non-thread safe model working as:
class var sharedInstance: TPScopeManager {
get {
...
553
votes
32
answers
419k
views
How to determine the current iPhone/device model?
Is there a way to get the device model name (iPhone 4S, iPhone 5, iPhone 5S, etc) in Swift?
I know there is a property named UIDevice.currentDevice().model but it only returns device type (iPod touch,...