-1

Why am I getting this error? I paste the code, my first attemp broke a thread when in runtime. Gosh, now I see why the first question apple recruiters ask CS students is do you know apple code, or 'swift'...(meh)

The error says - lastPathComponent is unavailable: Use lastPathComponent on NSURL instead

    // get a list of all the png files in the app's images group
    let paths = NSBundle.mainBundle().pathsForResourcesOfType(
        "png", inDirectory: nil) 

    // get image filenames from paths
    for path in paths  {
        if !path.lastPathComponent.hasPrefix("AppIcon") { //error
            allCountries.append(path.lastPathComponent)  //error
        }
    }

Attempt 1:

    for path in paths as [AnyObject] {
        if !path.lastPathComponent.hasPrefix("AppIcon") {
            allCountries.append(path.lastPathComponent)
        }
    }
Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
Chris Rathjen
  • 11
  • 2
  • 2
  • 9
  • This was due to the fact, that I was not able to use the original Xcode 5 for my deployment on my iphone – Chris Rathjen Oct 02 '15 at 18:29
  • Thanks for your email. The apps were written in Swift 1.2 and there are MANY breaking changes between Swift 1.2 and Swift 2.0. Xcode 7 requires Swift 2.0. – Chris Rathjen Oct 02 '15 at 21:38
  • You can still use lastPathComponent with NSString. Just cast (self as NSString).lastPathComponent or add an extension to your project so you don't need to change your code at all – Leo Dabus Oct 03 '15 at 01:31

1 Answers1

2

Use the URL related API

if let imageURLs = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
  // get image filenames from paths
  for url in imageURLs  {
    if !url.lastPathComponent!.hasPrefix("AppIcon") {
      allCountries.append(url.lastPathComponent!)
    }
  }
}

or a bit "Swiftier"

if let urls = NSBundle.mainBundle().URLsForResourcesWithExtension("png", subdirectory: nil) {
  allCountries = urls.filter {$0.lastPathComponent!.hasPrefix("AppIcon") == false} .map {$0.lastPathComponent!}
}
vadian
  • 274,689
  • 30
  • 353
  • 361