After converting the String values into Int values in an array, when I print to the logs, all I get is: [0, 0, 0, 0] when the output should be: ["18:56:08", "18:56:28", "18:57:23", "18:58:01"]
(without the quotations and the : colon).
I'm converting the string array, directly after the values have been added to the string array. I'm assuming that I'm not converting the values at the right time, or that my methods are placed wrong and that's why I get the 0 0 0 0 output.
Here is my ViewController code:
class FeedTableViewController: UITableViewController {
var productName = [String]()
var productDescription = [String]()
var linksArray = [String]()
var timeCreatedString = [String]()
var minuteCreatedString = [String]()
var intArray = Array<Int>!()
override func viewDidLoad() {
super.viewDidLoad()
var query = PFQuery(className: "ProductInfo")
query.findObjectsInBackgroundWithBlock ({ (objects, error) -> Void in
if let objects = objects {
self.productName.removeAll(keepCapacity: true)
self.productDescription.removeAll(keepCapacity: true)
self.linksArray.removeAll(keepCapacity: true)
self.timeCreatedString.removeAll(keepCapacity: true)
for object in objects {
self.productName.append(object["pName"] as! String)
self.productDescription.append(object["pDescription"] as! String)
self.linksArray.append((object["pLink"] as? String)!)
// This is where I'm querying and converting the date:
var createdAt = object.createdAt
if createdAt != nil {
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/YYY/HH/mm/ss"
let string = dateFormatter.stringFromDate(createdAt as NSDate!)
var arrayOfCompontents = string.componentsSeparatedByString("/")
self.timeCreatedString.append("\(arrayOfCompontents[0]) \(arrayOfCompontents[1]) \(arrayOfCompontents[2])")
self.minuteCreatedString.append("\(arrayOfCompontents[3]):\(arrayOfCompontents[4]):\(arrayOfCompontents[5])")
self.intArray = self.minuteCreatedString.map { Int($0) ?? 0}
print("INT ARRAY \(self.intArray)")
print(self.minuteCreatedString.map { Int($0) ?? 0})
print(self.minuteCreatedString)
}
self.tableView.reloadData()
}
}
})
}
When I tried this in another ViewController in the viewDidLoad method without Parse/queries happening, I get the correct output: a converted array of Ints. I'm assuming there's an issue of when and where i'm converting the Strings into Ints.
In what order/where should I convert the array of Strings into an array of Ints? Should I convert from Date to Int instead? If so, how do I do that? Am i doing something else wrong? I'm awfully confused....
Any help is very much appreciated!
If you have an NSDate object anyway, you can create the date string with the date formatter
let createdAt = NSDate() // or give date object
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
let string = dateFormatter.stringFromDate(createdAt) // "18:56:08"
Or if you want the integer values of hours, minutes and seconds, use NSDateComponents:
let comps = NSCalendar.currentCalendar().components([.Hour, .Minute, .Second], fromDate: createdAt)
let hour = comps.hour
let minute = comps.minute
let seconds = comps.second
let intArray = [hour, minute, second]
Related
I want retrieve core data and print the entire array in the log file. Right now my function converts a array to a double and then finds the average of all the numbers All I want to do is print the array of all the elements of the string "ee" in the log file
func performAction() {
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Data")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
var retrievedData = [Double]()
for data in result as! [NSManagedObject] {
if let value = data.value(forKey: "ee") as? String {
retrievedData.append(Double(value) ?? 0)
}
}
let arraySum = retrievedData.reduce(0, +)
print(arraySum/Double(retrievedData.count))
let venus = (arraySum/Double(retrievedData.count))
average.text = String("\(venus)")
} catch {
print("Failed")
}
}
I wrote an API which returns the username and amount and at what time the transaction was placed. I got the response from the backend as an array of objects and I stored it as NSArray in client side. And I want to attach that response to labels so I converted that NSArray into String and In Database some fields are empty so the response returns the null values whenever the response returns the null values I am getting this error like "Could not cast the value of NSNUll to NSString". What I thought is replace the null value in that array with string. I tried so much but it always shows the same error how to resolve that problem. This is my code :
let response = JSON as! NSDictionary
//example if there is an id
let deyaPaybalance = response.object(forKey: "Details")
print(deyaPaybalance!)
let Amount1:[Double] = (response.object(forKey: "Amount")) as! [Double]
print("Amount is",Amount1)
for ele:Double in Amount1 {
self.amo += [String(ele)]
}
print("amount is in string",self.amo)
let time = response.object(forKey: "Time") as! NSArray
print("time is", time)
let id = response.object(forKey: "TransactionID")
print("id is",id!)
let name:NSArray = response.object(forKey: "RName") as!NSArray
print("name is",name)
// let len = name.count
for (object) in name.enumerated() {
if let i = name.index(of: "null") {
print("hey it's nnull")
//name.index(of: "null")
}
else {
print("hello")
}
}
print("name",name)
// It is used to get the date from the time tsamp
for element in time {
let ele = element
let formatter = ISO8601DateFormatter()
//print("date is",date1!)
formatter.formatOptions = [.withFullDate,
.withTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime]
self.date2 = formatter.date(from: ele as! String)!
self.anotherFormatter.dateFormat = "MMM dd yyyy, h:mm a" // It is used to to show the date in th form of month year and time
self.anotherFormatter.string(from: self.date2)
let b = self.anotherFormatter.string(from: self.date2)
let final = b.replacingOccurrences(of: ",", with: " ")
let trimmedString = final.trimmingCharacters(in: .whitespaces)
self.dt += [trimmedString] // it is stored the final date and time
print("final tim is",trimmedString)
}
// End of the for loop
print(self.dt)
let contactViewController = self.storyboard?.instantiateViewController(withIdentifier: "TransactionDetails")as! TransactionDetails
contactViewController.method = deyaPaybalance as! [String]
contactViewController.amount = self.amo
contactViewController.timestamp = self.dt
contactViewController.transactionid = id as! [String]
contactViewController.name = name as! [String]// I am getting error here.
self.navigationController?.pushViewController(contactViewController, animated: true)
self.dismiss(animated: false, completion: nil)
UIApplication.shared.endIgnoringInteractionEvents()
You need to use like this to handle null in swift.
if let arr_name = name as? [String]{
contactViewController.name = arr_name
}else{
contactViewController.name = []
}
For replacing nil value in [String]
var name = ["a",nil,"3"]
if let nil_index = name.index(of: nil){
name[nil_index] = "empty"
}
print(name as! [String]) // Output is ["a", "empty", "3"]
I want to use the variables or array outside of the if statement. In javascript, you can hoist the variables outside of the if statement, but not sure how to do it in swift? I have tried using a struct, but I am not sure which direction to go in. I want to use num in a function and increment through each array. But, need to be able to access variables through the file.
let jsonWithArrayRoot = try? JSONSerialization.jsonObject(with: fileData, options: [])
var zone: Int
var sound: String
var distance: Int
var title: String
var duration: Int
var type: String
var color: Int
var num:Int
num=0
if let array = jsonWithArrayRoot as? [AnyObject] {
let json = array[num]
test = array
zone = json["zone"] as? Int ?? 0
sound = json["sound"] as? String ?? ""
distance = json["distance"] as? Int ?? 0
title = json["title"] as? String ?? ""
duration = json["duration"] as? Int ?? 0
type = json["type"] as? String ?? ""
color = json["color"] as? Int ?? 0
print(zone)
}
//I want to access variables and later on
print(zone)
print(sound)
print(distance)
print(title)
print(type)
print(color)
Use guard.
guard let array = jsonWithArrayRoot as? [AnyObject] else { return }
let json = array[num]
zone = json["zone"] as? Int ?? zone
sound = json["sound"] as? String ?? sound
distance = json["distance"] as? Int ?? distance
title = json["title"] as? String ?? title
duration = json["duration"] as? Int ?? duration
type = json["type"] as? String ?? type
color = json["color"] as? Int ?? color
Use array variable anywhere now.
You should give your variables default values right away, and then change them later based on the JSON data. Your current code puts the default values inside the JSON parsing block, which means that the variables would have no value if the if let array = jsonWithArrayRoot... fails.
Personally, I would also refactor the as? Int ?? lines to something prettier, but that's another issue.
Here's your code with a minimal fix that should work nicely:
let jsonWithArrayRoot = try? JSONSerialization.jsonObject(with: fileData, options: [])
var zone = 0
var sound = ""
var distance = 0
var title = ""
var duration = 0
var type = ""
var color = 0
var num = 0
if let array = jsonWithArrayRoot as? [AnyObject] {
let json = array[num]
zone = json["zone"] as? Int ?? zone
sound = json["sound"] as? String ?? sound
distance = json["distance"] as? Int ?? distance
title = json["title"] as? String ?? title
duration = json["duration"] as? Int ?? duration
type = json["type"] as? String ?? type
color = json["color"] as? Int ?? color
}
Ok, so I have an app that is designed for roleplaying. I have a guide that guides the user to creating the character. So the first thing the user has to do is put the character number. So I want it to search all the records for the attribute I have called characternumber and see what the largest number is.
Example: Lets say I have 5 characters. with character numbers 1,2,3,4,5. I want to cycle through all the records and see that 5 is the biggest number than automatically place a 6 in the character number text field.
This is what I have so far:
#IBOutlet var societyNumberTxt: UITextField!
#IBOutlet var characterNumberTxt: UITextField!
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var characters: [NSManagedObject] = []
var societyNum: [NSManagedObject] = []
var charNum: [String] = []
override func viewDidLoad()
{
super.viewDidLoad()
//1
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
//2
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Characters")
//3
do {
societyNum = try managedContext.fetch(fetchRequest)
let entityDescription = NSEntityDescription.entity(forEntityName: "Characters", in: managedObjectContext)
let request: NSFetchRequest<Characters> = Characters.fetchRequest()
request.entity = entityDescription
var results = try managedObjectContext.fetch(request as! NSFetchRequest<NSFetchRequestResult>)
if societyNum.count > 0
{
let match = results[0] as! NSManagedObject
societyNumberTxt.text = (match.value(forKey: "societynumber") as? String)!
print(match)
if (match.value(forKey: "characternumber") != nil)
{
self.charNum = match.value(forKey: "characternumber") as! [String]
print(self.charNum)
}
else
{
print("empty array")
characterNumberTxt.text = "1"
}
}
else
{
societyNumberTxt.placeholder = "Society # not set"
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
I am not sure how to cycle through every record and check the attribute and place in the array. I have tried something like this:
var i = 0
for i in results
{
var match = results[i] as? NSManagedObject
charNum[i] = match
}
I get the error:
cannot subscript a value of type '[Any]' with an index of type 'Any'
Now to test my code for the if statement:
if (match.value(forKey: "characternumber") != nil)
{
self.charNum = match.value(forKey: "characternumber") as! [String]
print(self.charNum)
}
It returned and error:
Could not cast value of type 'NSTaggedPointerString' (0x108578d10) to 'NSArray' (0x108578dd8).
Am I even on the right past?
Adjust your fetch request with a SortDescriptor instead of finding the highest characterNumber manually:
...
//2
// get all characters ...
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Characters")
// ... sorted by characternumber in acending order
let sortDescriptor = NSSortDescriptor(key: "characternumber", ascending: true)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = sortDescriptors
//3
do {
let characters = try managedContext.fetch(fetchRequest) as! [Characters]
if let highestCharacterNumber = characters.last?.characternumber {
characterNumberTxt.text = String(highestCharacterNumber + 1) // assuming characternumber is of type Int
} else {
characterNumberTxt.text = "1"
}
} ...
This block of code was working and now it's not. I get the error "Ambiguous use of 'subscript'" on the lat and long variables. What's going on? Is this because of a Swift update?
func showPrecincts() {
var urlBoundaries = "http://www.oklahomadata.org/boundary/1.0/boundary/?contains=" + "\(coords!.latitude)" + "," + "\(coords!.longitude)" + "&sets=precincts"
Alamofire.request(.GET, urlBoundaries, parameters: ["foo": "bar"])
.responseJSON { response in
if let data = response.result.value {
let nestedCoordinates = data.valueForKeyPath("objects.simple_shape.coordinates") as! Array<AnyObject>
let bug1 = nestedCoordinates.first as! Array<AnyObject>
let bug2 = bug1.first as! Array<AnyObject>
let coordinates = bug2.first as! Array<AnyObject>
var convertedCoords: [CLLocationCoordinate2D] = []
for individualCoordinates in coordinates {
let lat = (individualCoordinates[1] as! Double)
let long = (individualCoordinates[0] as! Double)
var newCoords = CLLocationCoordinate2DMake(lat, long)
convertedCoords.append(newCoords)
}
print(convertedCoords)
}
coordinates is cast to an array of AnyObject.
The compiler does not know that it's actually an array of Double in another array.
Downcast coordinates to Array<[Double]>
let coordinates = bug2.first as! Array<[Double]>
then you can get the elements without further type casting
let lat = individualCoordinates[1]
let long = individualCoordinates[0]