Could someone Please help.
I am reading "Learn IOS 8 App Development" and I have hit a road block on a chapter that explains userDefaults. The author gives an example extending a MapKit MKPointAnnotation but I'm getting the following error with the get{ } portion of the new property propertyState where it returns the dictionary. Would anyone know why it's throwing this error. "Type of expression is ambiguous without more context"
Also on the set{ } block I had to change the "title" from title = newValue[LocationKey.Title.rawValue] as NSString. To
title = newValue[LocationKey.Title.rawValue] as! NSString as String. Note: I came to this by following the Fixit popups. Is this correct? I don't
understand why (if this is correct) I would cast the newValue to NSString and then to String.
Thanks
Dave.
import MapKit
enum LocationKey: String {
case Latitude = "lat"
case Longitude = "long"
case Title = "title'"
}
extension MKPointAnnotation {
var propertyState: [NSObject: AnyObject] {
get {
return [ LocationKey.Latitude.rawValue: NSNumber(double: coordinate.latitude),
LocationKey.Longitude.rawValue: NSNumber(double: coordinate.longitude),
LocationKey.Title.rawValue: title ]
}
set {
let lat = (newValue[LocationKey.Latitude.rawValue] as! NSNumber).doubleValue
let long = (newValue[LocationKey.Longitude.rawValue] as! NSNumber).doubleValue
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
title = newValue[LocationKey.Title.rawValue] as! NSString as String
}
}
}
First Answer.
Swift warns for this sort of assignment:
title = newValue[LocationKey.Title] as! String
Treating a forced downcast to 'String' as optional will never produce 'nil'
I just wanted to avoid warning generating code. (I tried to explain why Swift warns us in this case, but had given up, that might seem more of a guess than an explanation.)
Second Answer.
Was this a good decision or should I have started with Objective C ( which I know nothing about right now)?
Yes, it was a good decision.
Swift has some features common with C#. I have no doubt about C# affected the design of Swift language (as well as some other languages), so you feel more familliar with Swift than Objctive-C.
`Similar but not same` makes some part of your learning tough, or you may need to learn Objective-C when you start to write practical apps, but I believe such things are in an acceptable range.
Is Core Data a good way to persist data
Yes. I do both client side and server side. I've been working with SQL Server, MySQL, PostgreSQL, DB/2 or Oracle... when writing server side code. But I choose Core Data to store data, even if it seems to fit for a simple db table, in iOS apps.
Third Answer.
Do IOS app's only connect to database's using API's.
Yes. It is not because iPhone can't connect directly to a network, but because it is an established and preferred way of providing database functionality to clients. As I wrote in another thread, connecting client devices directly to DBMS is thought to be outdated and should be avoided (mainly for security reasons).
As you know, I simplified many aspects and the answers are just my answers, my opinion.