get value of nested array alamofire swift 3 - nested

I need to get the nested array values, I need the user data and I need token data. I need this data because I need to initialize session variables to be able to use them in my application.
For example the user name, fotografia, managed to get the data rc and msg that are in the main array but I could not get the values ​​of nested arrays
["rc": 00,
"user": {
"__v" = 0;
deviceId = "";
email = "john#gmail.com";
fullName = "SMITH JOHN ";
lastName = "SMITH ";
modifiedAt = "2016-12-16T06:08:58.856Z";
name = "JOHN ";
photo = "";
provider = "";
"provider_id" = "";
status = 01;
tel = 3333333333;
typeUser = USER;
username = "john#gmail.com";
}, "token": {
"__v" = 0;
"_id" = 585384e3ccc4;
createdAt = "2016-12-16T08:10:03.407Z";
userId = 585384e3ccc4;
value = Z4WedlAzhdkap;
}, "msg": success]
#IBAction func btnLogin(_ sender: Any) {
let gsUtil=GSUtil()
let user = txtUser.text!
let password = txtPass.text!
let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(gsUtil.getCompleteURI()+"user/auth/",
method: .post,
parameters: nil,
encoding: URLEncoding.default,
headers:headers)
.validate()
.responseJSON { response in
if response.result.value != nil{
let json = response.result.value as? [String: Any]
let rc=json?["rc"]
let msg=json?["msg"]
if rc as? String=="00"
{
self.showMessage(msg: msg as! String)
}
}else{
self.showMessage(msg: "Error en los datos")
}
}
}

I do not really get the problem. You already pretty much did what you need to do (only one step further):
I would start by getting rid of all the pesky optionals using a lot of if lets:
if let json = input as? [String: Any] {
let msg = json["msg"]
if let rc = json["rc"] as? String, rc == "00" {
self.showMessage(msg: msg as! String)
}
if let user = json["user"] as? [String : Any] {
print(user)
}
if let token = json["token"] as? [String : Any] {
print(token)
}
} else {
self.showMessage(msg: "Error en los datos")
}

Related

Highlight only one word of a string [duplicate]

Hello I'm new to Swift and am using SwiftUI for my project where I download some weather data and I display it in the ContentView().
I would like to highlight some part of the Text if it contains some specific word, but I don't have any idea how to start.
In ContentView(), I have tried to set a function receiving the string downloaded from web and return a string. I believe this is wrong, because SwiftUI does not apply the modifiers at the all for the Text.
For example, in my ContentView() I would like the word thunderstorm to have the .bold() modifier:
struct ContentView: View {
let testo : String = "There is a thunderstorm in the area"
var body: some View {
Text(highlight(str: testo))
}
func highlight(str: String) -> String {
let textToSearch = "thunderstorm"
var result = ""
if str.contains(textToSearch) {
let index = str.startIndex
result = String( str[index])
}
return result
}
}
If that requires just simple word styling then here is possible solution.
Tested with Xcode 11.4 / iOS 13.4
struct ContentView: View {
let testo : String = "There is a thunderstorm in the area. Added some testing long text to demo that wrapping works correctly!"
var body: some View {
hilightedText(str: testo, searched: "thunderstorm")
.multilineTextAlignment(.leading)
}
func hilightedText(str: String, searched: String) -> Text {
guard !str.isEmpty && !searched.isEmpty else { return Text(str) }
var result: Text!
let parts = str.components(separatedBy: searched)
for i in parts.indices {
result = (result == nil ? Text(parts[i]) : result + Text(parts[i]))
if i != parts.count - 1 {
result = result + Text(searched).bold()
}
}
return result ?? Text(str)
}
}
Note: below is previously used function, but as commented by #Lkabo it has limitations on very long strings
func hilightedText(str: String) -> Text {
let textToSearch = "thunderstorm"
var result: Text!
for word in str.split(separator: " ") {
var text = Text(word)
if word == textToSearch {
text = text.bold()
}
result = (result == nil ? text : result + Text(" ") + text)
}
return result ?? Text(str)
}
iOS 13, Swift 5. There is a generic solution described in this medium article. Using it you can highlight any text anywhere with the only catch being it cannot be more then 64 characters in length, since it using bitwise masks.
https://medium.com/#marklucking/an-interesting-challenge-with-swiftui-9ebb26e77376
This is the basic code in the article.
ForEach((0 ..< letter.count), id: \.self) { column in
Text(letter[column])
.foregroundColor(colorCode(gate: Int(self.gate), no: column) ? Color.black: Color.red)
.font(Fonts.futuraCondensedMedium(size: fontSize))
}
And this one to mask the text...
func colorCode(gate:Int, no:Int) -> Bool {
let bgr = String(gate, radix:2).pad(with: "0", toLength: 16)
let bcr = String(no, radix:2).pad(with: "0", toLength: 16)
let binaryColumn = 1 << no - 1
let value = UInt64(gate) & UInt64(binaryColumn)
let vr = String(value, radix:2).pad(with: "0", toLength: 16)
print("bg ",bgr," bc ",bcr,vr)
return value > 0 ? true:false
}
You can concatenate with multiple Text Views.
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View{
let testo : String = "There is a thunderstorm in the area"
let stringArray = testo.components(separatedBy: " ")
let stringToTextView = stringArray.reduce(Text(""), {
if $1 == "thunderstorm" {
return $0 + Text($1).bold() + Text(" ")
} else {
return $0 + Text($1) + Text(" ")
}
})
return stringToTextView
}
}
PlaygroundPage.current.setLiveView(ContentView())
If you are targeting iOS15 / macOS12 and above, you can use AttributedString. For example:
private struct HighlightedText: View {
let text: String
let highlighted: String
var body: some View {
Text(attributedString)
}
private var attributedString: AttributedString {
var attributedString = AttributedString(text)
if let range = attributedString.range(of: highlighted)) {
attributedString[range].backgroundColor = .yellow
}
return attributedString
}
}
If you want your match to be case insensitive, you could replace the line
if let range = attributedString.range(of: highlighted)
with
if let range = AttributedString(text.lowercased()).range(of: highlighted.lowercased())
The answer of #Asperi works well. Here is a modified variant with a search by array of single words:
func highlightedText(str: String, searched: [String]) -> Text {
guard !str.isEmpty && !searched.isEmpty else { return Text(str) }
var result: Text!
let parts = str.components(separatedBy: " ")
for part_index in parts.indices {
result = (result == nil ? Text("") : result + Text(" "))
if searched.contains(parts[part_index].trimmingCharacters(in: .punctuationCharacters)) {
result = result + Text(parts[part_index])
.bold()
.foregroundColor(.red)
}
else {
result = result + Text(parts[part_index])
}
}
return result ?? Text(str)
}
Usage example:
let str: String = "There is a thunderstorm in the area. Added some testing long text to demo that wrapping works correctly!"
let searched: [String] = ["thunderstorm", "wrapping"]
highlightedText(str: str, searched: searched)
.padding()
.background(.yellow)
You can also make AttributedString with markdown this way
do {
return try AttributedString(markdown: foreignSentence.replacing(word.foreign, with: "**\(word.foreign)**"))
} catch {
return AttributedString(foreignSentence)
}
and just use Text
Text(foreignSentenceMarkdown)

Is there any way to let struct type array write to file?

I just want to write a struct type array into a file, but after I write it, the file is not created without any error message!!!???
The code :
struct temp {
var a : String = ""
var b : Date = Date()
init(
a : String = "",
b : Date = Date(),
) {
self.a = ""
self.b = Date()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var b = [temp]()
var c = temp()
c.a = "John"
c.b = Date()
b.append(c)
c.a = "Sally"
b.append(c)
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent("testFile")
do{
(b as NSArray).write(to: fileURL, atomically: true)
}catch{
print(error)
}
}
getTheFile()
}
func getTheFile() {
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent("testFile")
do {
print(try String(contentsOf: fileURL))
}catch{
print("read error:")
print(error)
}
}
}
It has a error message in getTheFile()
read error:
Error Domain=NSCocoaErrorDomain Code=260 "The file “ testFile” couldn’t be opened because there is no such file.
You cannot write custom structs to disk. You have to serialize them.
The easist way is the Codable protocol and PropertyListEncoder/Decoder
struct Temp : Codable { // the init method is redundant
var a = ""
var b = Date()
}
var documentsDirectory : URL {
return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var b = [Temp]()
var c = Temp()
c.a = "John"
c.b = Date()
b.append(c)
c.a = "Sally"
b.append(c)
let fileURL = documentsDirectory.appendingPathComponent("testFile.plist")
do {
let data = try PropertyListEncoder().encode(b)
try data.write(to: fileURL)
getTheFile()
} catch {
print(error)
}
}
func getTheFile() {
let fileURL = documentsDirectory.appendingPathComponent("testFile.plist")
do {
let data = try Data(contentsOf: fileURL)
let temp = try PropertyListDecoder().decode([Temp].self, from: data)
print(temp)
} catch {
print("read error:", error)
}
}
Note:
In Swift never use the NSArray/NSDictionary APIs to read and write property lists. Use either Codable or PropertyListSerialization.
If the array's contents are all property list objects (like: NSString, NSData, NSArray, NSDictionary) then only you can use writeToFile method to write the array in a file.
Here in your implementation, the array contains structure which is a value type, not an object. This is why you are getting an error.

fetch core data string and place in a label (Swift4)

I am trying to call 2 different core data strings and place them each on separate labels. Right now I am getting the error Cannot invoke initializer for type 'init(_:)' with an argument list of type '([NSManagedObject])'. This error is coming from j1.text = String(itemsName). I added both view controllers for saving and displaying.
import UIKit
import CoreData
class ViewController: UIViewController {
#IBOutlet var j1 : UITextField!
#IBOutlet var j2 : UITextField!
#IBAction func save(){
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Team", in : context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
theTitle.setValue(j1.text, forKey: "score")
theTitle.setValue(j2.text, forKey: "alba")
do {
try context.save()
}
catch {
print("Tom Corley")
}
}}
class twoVC: UIViewController {
#IBOutlet var j1 : UILabel!
#IBOutlet var j2 : UILabel!
var itemsName : [NSManagedObject] = []
var itemsName2 : [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Team")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "score", ascending: true)]
let fetchRequest2 = NSFetchRequest<NSManagedObject>(entityName: "Team")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "alba", ascending: true)]
do {
itemsName = try context.fetch(fetchRequest)
itemsName2 = try context.fetch(fetchRequest2)
if let score = itemsName[0].value(forKey: "score") {
j1.text = (score as! String)
}
if let alba = itemsName2[0].value(forKey: "alba") {
j2.text = (alba as? String)
}
}catch {
print("Ashley Tisdale")
}
}}
Loop over the result from the fetch and append to a string that is then used as value for the label, this goes inside the do{...} where you do the fetch today. Note that I am only using one fetch request here.
itemsName = try context.fetch(fetchRequest)
var mergedScore: String = ""
var mergedAlba: String = ""
for item in itemsName {
if let score = item.value(forKey: "score") as? String {
mergedScore.append(score)
mergedScore.append(" ") //separator
}
if let alba = item.value(forKey: "alba") as? String {
mergedScore.append(alba)
mergedScore.append(" ") //separator
}
}
j1.text = mergedScore
j2.text = mergedAlba
Try this one it's Working for me Swift 4 I think You need to store the value as int which are used as sortDescriptor.
func FetchManagedObjectFromDatabaseForStoreData(Entity :NSEntityDescription) ->
[NSManagedObject]
{
let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
// Add Sort Descriptor
let sortDescriptor = NSSortDescriptor(key: "order", ascending: true)
let sortDescriptor1 = NSSortDescriptor(key: "is_favourite", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor,sortDescriptor1]
// Create Entity Description
fetchRequest.entity = Entity
let result : [NSManagedObject] = []
// Execute Fetch Request
do{
let result = try appDelegate.managedObjectContext.fetch(fetchRequest) as! [NSManagedObject]
if result.count > 0
{
return result
}
else
{
// return result
}
}
catch{
let fetchError = error as NSError
print(fetchError)
}
return result
}
For Fetch Data
// Create Entity Description
let entityDescription = NSEntityDescription.entity(forEntityName: "Your Entity Name Here", in: appDel.managedObjectContext)
let DataObject = FetchManagedObjectFromDatabaseForStoreData(Entity: entityDescription!)
//Convert Array of NSManagedObject into array of [String:AnyObject]
for item in DataObject{
let keys = Array(item.entity.attributesByName.keys)
// Here is your result
print((item.dictionaryWithValues(forKeys: keys) as NSDictionary).value(forKey: "id") as Any) // And so On Whatewer you Fetch
}

CORE DATA: memory not being released even after manually faulting and doing a managed object reset during xml parsing of large mb file

I am parsing large kml file(18 MB) using NSXMLParser and simultaneously storing coordinates of every place in core data, what i am observing is memory is not being released after parsing 3/4th of the file memory piles up and application crashes, i have tried most of the answers given on stackoverflow but were not useful. Any help is mostly welcomed.
var currentElement : String = ""
var place:Place?
var polygon:Polygon?
var placeName = ""
var shouldAddCoordinates = false
private lazy var privateManagedObjectContext: NSManagedObjectContext = {
var managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext.persistentStoreCoordinator
return managedObjectContext
}()
override init() {
super.init()
}
// Xml Parser delegate methods
func parserDidStartDocument(_ parser: XMLParser) {
print("..Xml parsing has started")
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
currentElement = elementName
if elementName == "Placemark" {
place = NSEntityDescription.insertNewObject(forEntityName: "Place", into: privateManagedObjectContext) as? Place
place?.placeName = ""
}
if elementName == "Polygon" {
polygon = NSEntityDescription.insertNewObject(forEntityName: "Polygon", into: privateManagedObjectContext) as? Polygon
let fetchRequest = NSFetchRequest<Place>(entityName: "Place")
fetchRequest.predicate = NSPredicate(format: "placeName == %#", placeName)
do{
let fetchResult = try privateManagedObjectContext.fetch(fetchRequest) as [Place]
polygon?.place = place
polygon?.placeName = placeName
shouldAddCoordinates = fetchResult.first?.placeName == placeName ? true:false
}
catch{}
}
if elementName == "coordinates" {
let fetchRequest = NSFetchRequest<Place>(entityName: "Place")
fetchRequest.predicate = NSPredicate(format: "placeName == %#", placeName)
let fetchResult = try? privateManagedObjectContext.fetch(fetchRequest) as [Place]
shouldAddCoordinates = fetchResult?.first?.placeName == placeName ? true:false
}
if elementName == "name"{
placeName = ""
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElement = elementName
if elementName == "coordinates" {
let fetchRequest = NSFetchRequest<Place>(entityName: "Polygon")
let sortDescriptor = NSSortDescriptor(key: "polygonID", ascending: true )
fetchRequest.sortDescriptors = [sortDescriptor]
let fetchResult = try? privateManagedObjectContext.fetch(fetchRequest as! NSFetchRequest<NSFetchRequestResult>) as! [Polygon]
if fetchResult?.count == 0{
polygon?.polygonID = 1
}
else{
polygon?.polygonID = Int64(fetchResult!.count)
}
polygon?.place = place
}
if elementName == "coordinates" || elementName == "Polygon" || elementName == "name"{
if self.privateManagedObjectContext.hasChanges{
privateManagedObjectContext.performAndWait {
do{
try self.privateManagedObjectContext.save()
}
catch{}
}
}
}
else if elementName == "Placemark"{
placeName = ""
privateManagedObjectContext.performAndWait {
do{
try self.privateManagedObjectContext.save()
}
catch{}
}
privateManagedObjectContext.reset()
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElement == "name" {
if string == "\n" {
return
}
place?.placeName? += string
placeName += string
}
if currentElement == "coordinates"{
if shouldAddCoordinates {
privateManagedObjectContext.performAndWait{
if string == "\n" {
return
}
else{
let coordinates = NSEntityDescription.insertNewObject(forEntityName: "Coordinates", into: self.privateManagedObjectContext) as! Coordinates
let longitude = (string.components(separatedBy: ",") as [NSString]).first!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) as NSString
let latitude = (string.components(separatedBy: ",") as [NSString]).last!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) as NSString
let latitudeDegrees : CLLocationDegrees = latitude.doubleValue
let longitudeDegrees: CLLocationDegrees = longitude.doubleValue
coordinates.latitude = latitudeDegrees as NSNumber?
coordinates.longitude = longitudeDegrees as NSNumber?
coordinates.coordinatesDate = Date() as NSDate
coordinates.polygon = self.polygon
print("\(String(describing: coordinates.polygon?.placeName!)) has coordinates \(coordinates.latitude!),\(coordinates.longitude!)")
}
}
}
}
}
func parserDidEndDocument(_ parser: XMLParser) {
print("..Parser has finished parsing..")
}
func getManagedObject() -> NSManagedObjectContext{
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
return managedObjectContext
}
func saveCoordinates(){
let url = Bundle.main.url(forResource: "IND_adm1_1", withExtension: "kml")
let xmlParser = XMLParser(contentsOf: url!)
xmlParser?.delegate = self
_ = xmlParser?.parse()
}

Core Data setting all attributes to nil for first time

I am using Xcode 8 with Swift 3
Core Data setting all the attributes to nil for first time on load with no relationships.
Here is the screenshot.
In Console, i am getting this if App is installed for first time. I have deleted and try to run & install from Xcode number of times. Every time it sets all the values to nil.
"author_id" = nil;
"channel_id" = 0;
"created_at" = nil;
desc = nil;
id = 0;
images = nil;
"is_approved" = 0;
"is_bookmarks" = 0;
"is_like" = 0;
"is_wired" = 0;
"like_count" = 0;
link = nil;
"meta_des" = nil;
"post_id" = 0;
pubDate = nil;
"publisher_id" = 0;
"rss_url" = nil;
"share_cat" = nil;
"share_title" = nil;
"tiny_url" = nil;
title = nil;
"title_tag" = nil;
type = nil;
"updated_at" = nil;
url = nil;
Don't understand whats happening there. Anyone has any idea or hint what am i doing wrong or missing here. And this is only happening for "NewsPost" Entity.
Here is the code where i am adding data to core data.
func addData(entityName: String, entityValues: Any) {
var contextt = NSManagedObjectContext()
if #available(iOS 10.0, *) {
contextt = self.persistentContainer.viewContext
} else {
contextt = self.managedObjectContext
}
/// add data
switch entityName {
case EntityName.NewsEntity:
let singlePost = entityValues as! NSDictionary
let id = singlePost.value(forKey: "id") as! NSNumber
let title = singlePost.value(forKey: "title") as! String
let description = singlePost.value(forKey: "description") as! String
let link = singlePost.value(forKey: "link") as! String
let url = singlePost.value(forKey: "url") as! String
let is_approved = singlePost.value(forKey: "is_approved") as! NSNumber
let meta_des = singlePost.value(forKey: "meta_des") as! String
let title_tag = singlePost.value(forKey: "title_tag") as! String
let share_cat = singlePost.value(forKey: "share_cat") as! String
let author_id = singlePost.value(forKey: "author_id") as! String
let type = singlePost.value(forKey: "type") as! String
let publisher_id = singlePost.value(forKey: "publisher_id") as! NSNumber
let channel_id = singlePost.value(forKey: "channel_id")
let share_title = singlePost.value(forKey: "share_title") as! String
let rss_url = singlePost.value(forKey: "rss_url")
let pubDate = singlePost.value(forKey: "pubDate") as! String
let created_at = singlePost.value(forKey: "created_at") as! String
let updated_at = singlePost.value(forKey: "updated_at") as! String
let like_count = singlePost.value(forKey: "like_count") as! NSNumber
let tiny_url = singlePost.value(forKey: "tiny_url") as! String
let publisher = singlePost.value(forKey: "publisher") as! NSDictionary
let pubName = publisher.value(forKey: "name") as! String
let post = NSEntityDescription.insertNewObject(forEntityName: "NewsPost", into: contextt) as! NewsPost
post.id = Int64(id)
post.title = title
post.desc = description
post.link = link
post.url = url
post.is_approved = Int16(is_approved)
post.meta_des = meta_des
post.title_tag = title_tag
post.share_cat = share_cat
post.author_id = author_id
post.type = type
post.publisher_id = Int64(publisher_id)
post.channel_id = (channel_id as? Int64) ?? 0
post.share_title = share_title
post.rss_url = rss_url as? String ?? ""
post.pubDate = pubDate
post.created_at = created_at
post.updated_at = updated_at
post.like_count = Int64(like_count)
post.tiny_url = tiny_url
let images = singlePost.value(forKey: "images") as! NSArray
do {
let data = try JSONSerialization.data(withJSONObject: images, options: .prettyPrinted)
let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
post.images = string as? String
} catch {
}
print("post data -\(post.managedObjectContext)")
break
case EntityName.Categories:
entity.setValue(entityValues[0], forKey: EntityName.Entities.catName)
break
default:
break
}
/// we save our entity
do {
try contextt.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
This is how i am fetching data from Core Data.
func fetchData(entityToFetch: String, completion: #escaping (_ result: [NSManagedObject]?)->()) {
var context = NSManagedObjectContext()
if #available(iOS 10.0, *) {
context = CoreDataStack().persistentContainer.viewContext
} else {
context = DataController().managedObjectContext
}
// Fetching core data
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityToFetch)
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request)
print("fetch results- \(results as! [NSManagedObject])")
if results.count > 0 {
completion(results as? [NSManagedObject])
} else {
completion(nil)
}
} catch let error {
completion(nil)
print("fetch error -\(error.localizedDescription)")
}
}
Thank in advance.

Resources