Swift Model Class - Use of undeclared type - Xcode 7 beta
The property does matter. Change your variable name to not the same with the class name like so because it has conflicts and the compiler does not know it should be class name or variable name:
var person: Person?
var doctor: Doctor?
var address: Address?
JSON Serialization crashing in swift
The error Xcode is giving you isn't very helpful, but it looks like you need to declare your error
variable a different way (more at Apple's documentation), and then make sure you handle the case of the dictionary coming back nil
:
var error: AutoreleasingUnsafePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data,
options:NSJSONReadingOptions.MutableContainers,
error: error) as? NSDictionary
if jsonResult {
// process jsonResult
} else {
// couldn't load JSON, look at error
}
swift programming NSErrorPointer error etc
These types and methods have changed a lot since Swift 1.
- The
NS
prefix is dropped - The methods now throw exceptions instead of taking an error pointer
- Use of NSDictionary is discouraged. Instead use a Swift dictionary
This results in the following code:
do {
let object = try JSONSerialization.jsonObject(
with: responseData,
options: .allowFragments)
if let dictionary = object as? [String:Any] {
// do something with the dictionary
}
else {
print("Response data is not a dictionary")
}
}
catch {
print("Error parsing response data: \(error)")
}
Of, if you don't care about the specific parsing error:
let object = try JSONSerialization.jsonObject(
with: responseData,
options: .allowFragments)
if let dictionary = object as? [String:Any] {
// do something with the dictionary
}
else {
print("Response data is not a dictionary")
}
Original Answer
Your NSError has to be defined as an Optional
because it can be nil:
var error: NSError?
You also want to account for there being an error in the parsing which will return nil
or the parsing returning an array. To do that, we can use an optional casting with the as?
operator.
That leaves us with the complete code:
var possibleData = NSJSONSerialization.JSONObjectWithData(
responseData,
options:NSJSONReadingOptions.AllowFragments,
error: &error
) as? NSDictionary;
if let actualError = error {
println("An Error Occurred: \(actualError)")
}
else if let data = possibleData {
// do something with the returned data
}
How can I print out Core Data save error in Swift
You have to pass a pointer to an optional NSError
object.
As an example, this is the template code from Xcode if you create an iOS app and select
the "Use Core Data" checkbox:
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// ...
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
Related Topics
Attrackingmanager Stopped Working in iOS 15
Type of Expression Is Ambiguous Without More Context in Xcode 11
File Couldn't Be Opened Because You Don't Have Permission to View It Error
Swift Struct with Lazy, Private Property Conforming to Protocol
Migration from Swift 3 to Swift 4 - Cannot Convert String to Expected String.Element
How to Trigger Updateuiview of a Uiviewrepresentable
How to Import Googleanalytics Header into a Library Framework
Mandatory Init Override in Swift Uinavigationcontroller Subclass
Counting Coloured Pixels on the Gpu - Theory
Integrate Existing Aws Cognito User Pool into iOS Project with Amplify
How to Define Static Constant in a Generic Class in Swift
How to Unwrap Arbitrarily Deeply Nested Optionals in Swift
Toolbar Is Deleting My Back Button in the Navigationview
Set Uitextfield Placeholder Color Programmatically
Gcdasyncsocket Multiple Connections Wont Accept Data from Multiple Sockets
Numeric Types Don't Automatically Bridge to Nsnumber in Pure Swift on Ubuntu Linux