How to Get Value from Array to Variable Swift - arrays

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"

Related

Fetch Core Data as comma separated list

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)
}

How to store multiple datas in an Array in swift 4?

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] = []

Swift3 how do I get the value of a specific key in a string?

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
}
}

Ambiguous use of subscript error only on real device

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
}

Swift array in dictionary leads to NSCFArray

I prepare a swift Array in my Watch Interface and send it to the iOS App:
#IBAction func buttonGeklickt() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm"
let datumString = dateFormatter.stringFromDate(NSDate())
var swiftArray = [String]()
swiftArray.append(datumString)
var swiftDict = ["a":swiftArray]
session.transferUserInfo(swiftDict)
}
so far so good, on the iOS App the dictionary arrives, but there seems to be something wrong with the Array in the Dictionary:
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
print ("seems to be the same Dict = \(userInfo)")
if let vw = userInfo["a"] as? [String: String] {
print ("Never called! Here I would expect my array from the watch \(vw)")
}
}
I would expect and like vw to hold the same array as swiftArray in the watchApp. However it seems to be of type __NSCFArray:
screenshot
So what I'm doing wrong here?
I'm new to Swift, however I'm experienced with Objective C to solve actually every problem I faced in the past years, but this issue seems to be so basic and it's embarrassing that I'm not able to solve it on my own. So help is much appreciated
If I understand your code correctly, you are saving "a" as value of type [String]. But you are trying to read it as [String:String]. Instead of
if let vw = userInfo["a"] as? [String: String]
try
if let vw = userInfo["a"] as? [String]

Resources