Coding Explorer Blog

Exploring how to code for iOS in Swift and Objective-C

  • Home
  • Apps
  • About
  • Contact

Archives for August 2014

Closure Expressions in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Closures are a very useful part of Swift.  An Int is a type that stores an Integer, a String is a type that holds a string.  In the same way, a closure is, basically, a type that holds a function.  With this capability, you can do many things with closures that you couldn’t do in several older languages.  You can assign closures to a variable, you can pass them as arguments to other functions, you can even return one from a function.

Closures in Swift are similar to blocks in Objective-C.  In fact, when you call an Objective-C API from Swift that wants a block, you pass in a closure.  The major difference between them is coding style, and that closure expressions are much easier to read and write in Swift.
[Read more…]

Filed Under: Swift

Custom Subscripts in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Last time we talked about Arrays in Swift, and the main way to acquire a value was to look it up via an index using subscripts.  Swift made it rather easy to implement subscripts in your own classes.  Sure you can use an Array or Dictionary type if you wanted, and in many cases you should, but if writing it in subscript syntax is clearer and more expressive, that is a good reason to use it.
[Read more…]

Filed Under: Swift

Arrays and their Methods in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 12, 2020

Arrays are a very common component in many programming languages.  Swift is no different.  Arrays are an ordered list of objects of the same type in Swift.  This is the case for arrays in most other programming languages, but Objective-C’s NSArray was capable of holding values of different types in the same NSArray.  In that case, they were stored as id, and thus took a bit more work to change them back into whatever type they actually were.  Swift simplifies it by enforcing that all values are of the same type.  This in particular makes things safer, so you don’t have to check what type a variable is when you take it out of a Swift array.
[Read more…]

Filed Under: Swift Tagged With: optionals

Swift Strings

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

Strings are a very common type of variable in many programming languages.  Strings are sequences of characters, used to store text.  Swift in particular works with them in a Unicode-compliant way.  In this post we will discuss some of the higher level aspects of working with Strings in Swift.  The Swift String is very different from its predecessor NSString, but they are bridged together in such a way that you can use a Swift string anywhere you would use an NSString.  You can also use NSString methods on Swift Strings.  In Swift 1.2, NSStrings are not automatically bridged to Swift Strings (but they were beforehand), they must be typecast using the “as” operator.  Swift Strings can still be used where an NSString is expected though, so you only have to typecast an NSString to its Swift equivalent if you still have one.

[Read more…]

Filed Under: Swift

Type Casting in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

In my previous post, Generic Functions in Swift, I used a type casting operator to check for inheritance.  It was simple enough to mention there, but I felt I should cover type casting in Swift a bit more in-depth in its own post.  Type casting is a way to convert an object of one type to another.

There is one term that is used a lot when talking about type casting, so it should probably be defined upfront, that term is downcast.  According to Wikipedia, downcasting is the act of casting a reference of a base class to one of its derived classes.  There is an opposite term to this one, the obviously named upcast.  While this term is not used in Apple’s iBook, it is used in the WWDC video “Swift Interoperability In Depth.”  It of course means to go the other way, casting a derived class back up to one of its base classes.
[Read more…]

Filed Under: Swift Tagged With: optionals, Swift

  • 1
  • 2
  • Next Page »

Subscribe to the Coding Explorer Newsletter

* indicates required

Follow Us

Facebooktwitterrss

Recent Posts

  • Error Handling in Swift
  • Creating and Modifying a URL in your Swift App
  • Watch Connectivity in Swift — Application Context
  • watchOS Hello World App in Swift
  • API Availability Checking in Swift

Categories

  • Class Reference
  • General
  • Interface Builder
  • My Apps
  • Objective-C
  • Swift
  • Syntax
  • Tutorial
  • Uncategorized

Archives

  • May 2016
  • March 2016
  • February 2016
  • December 2015
  • July 2015
  • June 2015
  • April 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • January 2014
  • November 2013
  • September 2013
  • August 2013
  • July 2013
  • Terms Of Use
  • Privacy Policy
  • Affiliate Disclaimer

Subscribe to the Coding Explorer Newsletter

* indicates required

Copyright © 2025 Wayne Media LLC · Powered by Genesis Framework · WordPress