Coding Explorer Blog

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

  • Home
  • Apps
  • About
  • Contact

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

Generic Functions in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

Now, with knowledge of Protocols in Swift in hand, let’s get to talking about one of the other big additions to the Swift language:  Generics.  I had previously discussed in my post Swift Optionals – Declaration, Unwrapping, and Binding how Swift optionals work under the hood.  In that case, the OptionalValue enum is a generic type.  Today we are going to cover a slightly easier aspect of generics, generic functions.  Generic functions are functions that can take parameters of any type (with or without constraints based on protocol adoption), and perform some action with them.
[Read more…]

Filed Under: Swift Tagged With: optionals

Protocols in Swift

Xcode 11.6 Swift 5.2.4

Last updated on August 10, 2020

I wanted to write a bit about Generics in Swift, but I realized that some of the major powers of Generics require the use of protocols, so I felt I should start by talking about them first.

In Swift, protocols are basically a named contract that your classes can conform to.  If your class says it conforms to Equatable, then it better fulfill all of the required functionality to make it equatable.  They are rather similar to interfaces in C# or Java.  They list off the function prototypes and variable declarations, as well as stating whether they are required or optional, but don’t actually do anything.  It is up to class, structs, or enumerations that claim to conform to the protocol to actually provide the functionality.

Defining a Protocol

This is pretty easy.  It is very much like defining a class in Swift, except you write protocol instead, observe:

protocol Vehicle {
    //Functions and properties prototypes go here.
}

Then you just fill it with what you want.
[Read more…]

Filed Under: Swift Tagged With: optionals, properties

  • « Previous Page
  • 1
  • …
  • 5
  • 6
  • 7
  • 8
  • 9
  • 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