How to search for a Character in a String with Swift 2

Working with Strings in Swift 2 has changed significantly. There are several big upcoming changes to how you work with String. 

1. String is no longer a collection type, instead the characters contained with it are accessed via a new property named characters (as mentioned on Apple’s blog).

2. Apple introduced Protocol Extensions in Swift 2 that will significantly change many APIs and functions. There’s a ton of movement from global functions (hard to find!) to methods (easier to find).

Swift 1.2 String Character Search

Searching for a Character in Swift 1.2 uses the global function contains() or find(), you won’t find them in Auto Complete – this greatly reduced the discovery of basic actions in Swift 1.2.

Using the lowercaseString property allows you to make the search case-insensitive.

// Swift 1.2
var word = "Apple"
var searchCharacter: Character = "p"

if contains(word.lowercaseString, searchCharacter) {
    print("word contains \(searchCharacter)")
}

if let index = find(word.lowercaseString, searchCharacter) {
    print("Index: \(index)")
}

To make character search work for both upper and lower case, you can use the lowercaseString property on the String.

Swift 2 String Search

In Swift 2 global many functions are gone and replaced with new instance methods. Some of these transformations were one-to-one and others were renamed. Some error messages can suggest how to change your code, otherwise you may have to dig.

The contains() function has been replaced by the contains() method that can be invoked on the characters property of the new Swift 2 String.

The find() function has been replaced with a new method called indexOf() that works on your characters property.

// Swift 2
var word = "Apple"
var searchCharacter: Character = "p"

if word.lowercaseString.characters.contains(searchCharacter) {
    print("word contains \(searchCharacter)")
}

if let index = word.lowercaseString.characters.indexOf(searchCharacter) {
    print("Index: \(index)")
}

Questions: the evolution of Swift

Working with Strings and Characters is fundamental for any programming language – when it’s hard to find the methods or the language uses different conventions it can be hard to figure out what code to write next.

What other functions, methods, or code tasks have you struggled with in Swift?

Reply below or on Twitter: @PaulSolt

 

WatchKit Swift Tutorial - Download Xcode 6.2 Beta and Start Making Apple Watch Apps

The first beta of the WatchKit SDK was launched this past week and developers can now start making apps.

There are some limitations, but Apple released a lot more than was expected for WatchKit. There's a lot you'll be able to do within the restrictions, which are related to the embedded hardware (i.e.: Apple Watch) and it's battery life.

For now, until Native Apple Watch support is added sometime next year (fingers crossed), you'll have to work within the provided mechanisms. If something doesn't work, you'll need to let Apple know by submitting bug reports, feature requests, or enhancements to WatchKit or Xcode.

Known Limitations of WatchKit in Xcode 6.2 Beta

  1. Updated: (Beta 2 allows Watch apps to trigger iPhone apps) You cannot press a button on Apple Watch to cause an action in your iPhone app.
  2. You cannot create or use gestures. All gestures are controlled by Apple.
  3. You cannot move objects around screen by CGPoint position (Maybe with tableview hide/show).
  4. You cannot add UI elements at run-time, everything is design time before app runs.
  5. You cannot make fluid 2D/3D games like Flappy birds (Maybe jittery gameplay with image animations + hiding/showing hackery, + borderless button).
  6. Animations are image-based (in Apple's sample they include 360 images to show a full circle progress bar = 2mb image data).
  7. Your code runs on your iPhone app in an extension (new in iOS 8), and your UI + images reside on the Apple Watch.
  8. Your app will stop if the Apple Watch and iPhone are separated beyond the range of bluetooth. Only Apple can run native apps on device.
  9. You can only use storyboard files for UI (no .xib files).
  10. You must create an iPhone app Xcode project and add Apple Watch targets to create an app.
  11. You cannot use a physical iPhone with the Apple Watch simulator. Only available in simulation on Mac.

With some of the limitations in perspective, you can design apps that provide meaningful information that compliment your iPhone app. The watch is intended to show information that can be used quickly, and it isn't designed as a gaming device because of it's limited battery and processing power.

Download and install Xcode 6.2 Beta from the Apple Developer Website

Go to https://developer.apple.com/watchkit/ and you can download the beta versions of Xcode 6.2 and iOS 8.2 for testing out features relating to Apple Watch.

Scroll down to the bottom and read both the Apple Watch Design Guide and the Apple Watch Programming Guide.

Download Xcode 6.2 Beta using this link.

Getting Started with WatchKit in Xcode 6.2 Beta

In order to bring you up to speed, I've created a short YouTube video series in 4K resolution that demonstrates how to create a WatchKit app using timers. There are some bugs, but this is the best place to start if you want to work with some cutting edge technology.

Please submit any bug reports to http://bugreport.apple.com and you can help Apple improve WatchKit. Duplicate bug requests are a good thing, because it helps Apple see the more common issues.

Download the Xcode project with the Apple Watch app from github.

Swift WatchKit Tutorial 1 - Get Started with WatchKit to Make apps for the Apple Watch

WatchKit Swift Tutorial 2 - Getting Started with your First WatchKit App in Xcode 6 2 Beta

WatchKit Swift Tutorial 3 - Apple Watch User Interface Explained

WatchKit Swift Tutorial 4 - Connect the Apple Watch User Interface to Code

WatchKit Swift Tutorial 5 - Setup a NSTimer in Swift with Apple Watch

WatchKit Swift Tutorial 6 - Synchronize NSTimer and WKInterfaceTimer

Connect