Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJeff Lange
4,791 PointsLots of errors in my init method: 'AnyObject?' is not convertible to 'String!'
Any time I use the playlistDictionary in my Playlist init in my Playlist.swift file, I get the error: 'AnyObject?' is not convertible to 'String!'
This happens on the title, description, iconName, largeIconName, artists, and colorsDictionary line.
//
// Playlist.swift
// Algorhythm01
//
// Created by Jeff Lange on 4/11/15.
// Copyright (c) 2015 Jeff Lange. All rights reserved.
//
import Foundation
import UIKit
struct Playlist {
var title: String?
var description: String?
var icon: UIImage?
var largeIcon: UIImage?
var artists: [String] = []
var backgroundColor: UIColor = UIColor.clearColor()
init(index: Int) {
let musicLibrary = MusicLibrary().library
let playlistDictionary = musicLibrary[index]
title = playlistDictionary["title"] as String!
description = playlistDictionary["description"] as String!
let iconName = playlistDictionary["icon"] as String!
icon = UIImage(named: iconName)
let largeIconName = playlistDictionary["largeIcon"] as String!
largeIcon = UIImage(named: largeIconName)
artists += playlistDictionary["artists"] as [String]
let colorsDictionary = playlistDictionary["backgroundColor"] as [String: CGFloat]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
Jeff Lange
4,791 PointsThanks, Andre! I thought I was going crazy trying to track down what I did wrong.
2 Answers
Ethan Lowry
Courses Plus Student 7,323 PointsYou may need to place the !
after e.g. playlistDictionary["title"]
instead of after the String!
casting. My swift is a little rusty but at first glance it looks to me like when you access your dictionary you're getting an AnyObject
optional back, then trying to cast it as a String, which I believe can't be done with an optional. If you unwrap the dictionary contents first with e.g. !
, then maybe you'll be able to cast them to string.
Hope that helps.
Ian Stewart
4,255 PointsHi,
I'm using Xcode 6.0 and this seems to give no errors but I am not sure why?
struct Playlist {
var title: String?
var description: String?
var icon: UIImage?
var largeIcon: UIImage?
var artists: [String] = []
var backgroundColor: UIColor = UIColor.clearColor()
init(index: Int) {
let musicLibrary = MusicLibrary().library
let playlistDictionary = musicLibrary[index]
title = playlistDictionary["title"]! as? String
description = playlistDictionary["description"]! as? String
let iconName = playlistDictionary["icon"]! as? String
icon = UIImage (named: iconName!)
let largeIconName = playlistDictionary["largeIcon"]! as? String
largeIcon = UIImage (named: largeIconName!)
artists += playlistDictionary["artists"]! as [String]
}
}
This was more trial and error than anything though when it comes to the ! and ? uses!
Thanks
Ian
Andre Glegg
1,822 PointsAndre Glegg
1,822 PointsAdd ! after as... this had to be done after the most recent update to swift. eg. title = playlistDictionary["title"] as! String!