My code:
let userData = result["user"] as? AnyObject // { test="x"; id=0; phone="none" }
var id = Int(userData?["id"] as? String ?? String(userData?["id"] as? String ?? "0"))!
After compile i get this error :
2018-07-16 18:21:02.747944+0430 Matap[1550:415553] -[__NSCFNumber
length]: unrecognized selector sent to instance 0xb000000000000003
2018-07-16 18:21:02.748116+0430 Matap[1550:415553] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFNumber length]: unrecognized selector sent to instance
0xb000000000000003'
* First throw call stack: (0x183c82d8c 0x182e3c5ec 0x183c90098 0x183c885c8 0x183b6e41c 0x105d9d5f4 0x105d36c68 0x106384478
0x1020a4aa0 0x10209f6cc 0x10208ac00 0x10676d1dc 0x10676d19c
0x106771d2c 0x183c2b070 0x183c28bc8 0x183b48da8 0x185b2d020
0x18db65758 0x1020c9098 0x1835d9fc0) libc++abi.dylib: terminating with
uncaught exception of type NSException
result["user"] is not AnyObject, according to the output it's clearly a dictionary ([String:Any])
userData["id"] is an Int
The error occurs because you treat the number as String.
One-liners seem to be cool but sometimes it's preferable to use a few lines more for better readability 😉
let id : Int
if let user = result["user"] as? [String:Any], userId = user["id"] as? Int {
id = userId
} else {
id = 0
}
And for sake of coolness this is the one-liner
let id = (result["user"] as? [String:Any])?["id"] as? Int ?? 0
id is NSNumber
var id = (userData?["id"] as! NSNumber).stringValue // seems you consider it a string
Related
I realise that I am missing something simple but as a Swift newbie I am going around in circles & would appreciate a pointer as to what I am doing wrong?!
I have a Core Data Entity called "Numbers" with an attribute (Int16) called "userNumbers". I am fetching the results like:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Numbers")
//request.predicate = NSPredicate(format: "age = %#", "12")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
for data in result as! [NSManagedObject] {
print("\(data.value(forKey: "userNumbers") as! Int16)")
}
} catch {
print("Failed")
}
The result in my console is:
12
13
18
19
21
I need to know how to make this a comma separated list so I can use it in an array. Essentially I need the return to be: 12,13,18,19,21
Everything I try seems to be wrong!
First of all create a more specific fetch request to get a distinct result type
let request = NSFetchRequest<Numbers>(entityName: "Numbers")
A comma separated list is not possible because the type of userNumbers is numeric.
You can map the result to an array of Int16 with
do {
let result = try context.fetch(request) // the type is [Numbers]
let numberArray = result.map{$0.userNumbers}
print(numberArray)
}
I am getting a JSON from an API. So, I want to store a particular value with key SKU. So, what I did was:
var skuArr = [""]
{ (response, error) in
if (error != nil)
{
print("Error \(error.debugDescription)")
}
else
{
self.coinsArr = response.arrayObject as? Array<Dictionary<String, Any>>
for i in 0 ..< (self.coinsArr?.count)!
{
self.skuArr = [response[i]["sku"].rawValue as! String]
}
}
So, with this I am getting the array in skuArr, but when i is 0 I am getting ["a"], and when i is 1 I want it to be ["a","b"], but it gives ["b"] only and when the loop ends with only the last value and not with ["a","b","c","d"] which I want as the final result. How can I insert each of them in the Array?
First of all declare skuArr as empty string array.
var skuArr = [String]()
And this is Swift. There are better ways than ugly index based loops to extract data for example with map or compactMap
if let result = response.arrayObject as? Array<Dictionary<String, Any>> {
self.coinsArr = result
self.skuArr = result.compactMap{ $0["sku"] as? String }
}
And why is coinsArr declared as optional as you are going to force unwrap it anyway? It's highly recommended to use non-optional types as much as possible. Non-optionals can never cause a well-liked unexpected found nil crash
Don't use this:
self.skuArr = [response[i]["sku"].rawValue as! String]
As this will replace the previous value with new one.
Use .append to insert into array.
self.skuArr.append([response[i]["sku"].rawValue as! String])
EDIT
change your initialisation as below:
skuArr: [String] = []
I have a Questions
I want to move Value in array to Variable
ex.
[1,2,3] = array
i want to get "1" to Variable
Var = 1 <= Which "1" is Value in array
My code :
//Loop For Seach Value
for result in 0...DataSearch.count-1 {
let Object = DataSearch[result] as! [String:AnyObject];
self.IDMachine_Array.append(Object["IDMac"] as! String!);
self.Password_Array.append(Object["password"] as! String!);
self.Conpassword_Array.append(Object["password_con"] as! String!);
self.Tel_Array.append(Object["Tel"] as! String!);
self.Email_Array.append(Object["Email"] as! String!);
self.Email = String(self.Email_Array);
}
I try Value Email = Email_Array
Result print :
[xxxx#xxxx.com]
but i want Result is :
xxxx#xxxx.com -> without []
Please Help me please.
Thank you.
Sorry if my solution is wrong.
Just get the first element from the array?
self.Email = self.EmailArray.first!
(this is the same as self.Email = self.EmailArray[0])
NB: first! or [0] will both crash if the array is empty. The original question uses as! so obviously just need this to work. However, if you wanted safety you would use something like
if let email as self.EmailArray.first {
self.Email = email
}
or
self.Email = self.EmailArray.first ?? "no email found"
I've got a server response returning
(
{
agreementId = "token.virtual.4321";
city = AMSTERDAM;
displayCommonName = "bunch-of-alphanumeric";
displaySoftwareVersion = "qb2/ene/2.7.14";
houseNumber = 22;
postalCode = zip;
street = "";
}
)
how do I get the value of agreementId? response['agreementId'] is not working. i've tried some example code with .first but I cannot get it working.
Some extra information, I do a http call to a server with alamofire. I try to parse the json to a constant response:
let response = JSON as! NSDictionary
However that returns a error message
Could not cast value of type '__NSSingleObjectArrayI' (0x1083600) to 'NSDictionary' (0x108386c).
So now parse the json to an array, which seems to be working. The code above is what
let response = JSON as! NSArry
print(response)
spits out.
Now I only need to retrieve the value from the key "agreementId" and I have no clue how to do that.
In swift you need to use Swift's native type Array/[] and Dictionary/[:] instead of NSArray and NSDictionary, if you specify the type like above means more specific then the compiler won't complain. Also use optional wrapping with if let or guard let to prevent crash.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
if let dic = array.first {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}
Note: If you having more than one object in your array then you need to simply loop through the array to access each dictionary of array.
if let array = JSON as? [[String:Any]] {//Swift type array of dictionary
for dic in array {
let agreementId = dic["agreementId"] as? String ?? "N/A"//Set default value instead N/A
print(agreementId)
//access the other key-value same way
}
}
When I try to run the code below on my iPhone, I get the following error
rrr.swift:356:72: Ambiguous use of 'subscript'
The strange thing is it happens only when I want to run the app on the phone, on the simulator however it works fine.
There are 3 lines which cause the error where. I noted them below in the code in capital letters.
I try to access properties in an array inside the JSON array, I suspect Im doing it the wrong way but have no idea how to fix it.
How can these properties be retrieved without the subscript error?
self.restApi.getSchoolDetails(schoolId) {responseObject, error in
// use responseObject and error here
self.schoolDetailsCollection = NSDictionary(dictionary: responseObject! as! [String : AnyObject])
print(self.schoolDetailsCollection)
if let response = responseObject as? NSDictionary {
//parse the response object
self.schoolDetailsList = (response as? NSDictionary)!
//assigning all values to shareData class
self.shareData.sco_id = response["result"]!["sco_id"] as!NSInteger
self.shareData.address = response["result"]!["address"] as!String
self.shareData.name = response["result"]!["name"] as! String
print("school name")
print(self.shareData.name)
self.shareData.intro = response["result"]!["intro"] as! String
self.shareData.sell_point = response["result"]!["sell_point"] as! String
self.shareData.city = response["result"]!["city"] as! String
self.shareData.cou_id = response["result"]!["cou_id"] as! String
//get images from the nested array in the json array
/THESE THREE LINES CAUSE THE ERROR SUBSRCIPT
self.shareData.image1 = response["result"]!["images"][0]! as! String
self.shareData.image2 = response["result"]!["images"]![1] as! String
self.shareData.image3 = response["result"]!["images"]![2] as! String
print(self.shareData.image1)
print(self.shareData.image2)
print(self.shareData.image3)
//open next controller after all info has been set correctly
//info is being passed by Singleton class Sharedata
if let COBezierDemoViewController = self.storyboard!.instantiateViewControllerWithIdentifier("COBezierDemoViewController") as? COBezierDemoViewController {
self.presentViewController(COBezierDemoViewController, animated: true, completion: nil)
}
}
}
}
The JSON file:
{
result = {
address = "223 Vincent St, West Perth WA 6005, Australia";
city = Perth;
"cou_id" = AU;
"cur_id" = "";
environment = R;
financed = "<null>";
images = (
"Phoenix_Academy_1.jpeg",
"Phoenix_Academy_3.jpeg",
"Phoenix_Academy_2.jpeg"
);
intro = "Our language school in Perth has a modern campus and a spacious garden. The language school is located 10 minutes north from the city center. You can reach the city center and the famous bea
As you hint that error in this line:
self.shareData.image1 = response["result"]!["images"][0]! as! String
Error:
Error is [0] subscript which you are implementing in AnyObject.
response["result"]!["images"] // this is returning Optional<AnyObject> which do not have subscript option.
Correct way should be this:
Downcast AnyObject as? [String] // this will either return nil or an Optional Array<String?> where String is image name.
Without downcasting, you will get error: Ambiguous use of subscript.
let images = response["result"]? ["images"] as? [String]
images?.flatMap({ $0 }).forEach {
print($0) // Phoenix_Academy_1.jpeg, Phoenix_Academy_3.jpeg, Phoenix_Academy_2.jpeg
}