Swift get specific NSManagedObject from entity (core data) - swift

I have an entity in my "ProjName.xcdatamodel" with the name "Questions". In this entity I have 5 attributes ("icehockey","volleyball","soccer",...), each with type transformable. Each row (attribute) will be filled with a NSMutableArray.
What I want to do is to get the value of a specific attribute in this entity. This is my code:
func readQuestionsFromCore(sport:NSString) -> NSMutableArray {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Questions")
request.returnsObjectsAsFaults = false
var results: NSArray = context.executeFetchRequest(request, error: nil)!
var qArr:NSMutableArray!
if results.count > 0 {
var res = results[0] as NSManagedObject
qArr = res.valueForKey("\(sport)") as NSMutableArray
return qArr
} else {
qArr = []
return qArr
}
}
This will ofcourse not work since I take out the first index of the results from the database (results[0] as NSManagedObject) and thus it will crash if that element is not the same as the valueForKey I'm looking for.
How do I get the one result row that I'm looking for? I.e. "soccer", or at least can I somehow loop through the results and compare the keys of each result row so it doesn't crash when I try with the wrong key? Like something like this:
for (res) in results as NSManagedObject {
if(res.key == "soccer") {
qArr = res.valueForKey("soccer") as NSMutableArray
return qArr
}
}
I hope I'm clear in my explanation!

the valueForKey method returns an optional, you can use if let as below
if let questionsArr = res.valueForKey("\(sport)") as? NSMutableArray {
return questionsArr
} else {
return []
}
This works in Xcode 6.3.2, but looks like you are using older one. If so update to latest one.

I'm not sure I clearly understand what you are trying to achieve. But using next function(that using KVC) you can get a list of class properties and than check if the one you need is there:
func getPropertyList(#classToInspect: AnyObject.Type) -> [String]
{
var count : UInt32 = 0
let properties : UnsafeMutablePointer <objc_property_t> = class_copyPropertyList(classToInspect, &count)
var propertyNames : [String] = []
let intCount = Int(count)
for var i = 0; i < intCount; i++ {
let property : objc_property_t = properties[i]
let propertyName = NSString(UTF8String: property_getName(property))!
propertyNames.append(propertyName as String)
}
free(properties)
println(propertyNames)
return propertyNames
}

Related

How do I call a function with parameters?

I am using xcode playgrounds toying around with some code and I get this error Use of unresolved identifier 'colorArray' when I try to call a function. How do I fix this I know it has to be simple.
import Foundation
class Solution{
var colorArray = ["Red","blue","green","black","red","blue","yellow","purple","Red","Red","Red","yellow","yellow","black","green","black","purple","black","yellow","purple","purple","blue","yellow","blue","green","green","yellow","pink"]
func getMostCommonColor(array: [String]) -> [String] {
var topColors: [String] = []
var colorDictionary: [String: Int] = [:]
for color in array {
if let count = colorDictionary[color] {
colorDictionary[color] = count + 1
}else{
colorDictionary[color] = 1
}
}
let highestValue = colorDictionary.values.max()
for (color,count) in colorDictionary {
if colorDictionary[color] == highestValue {
topColors.append(color)
}
}
return topColors
}
}
let solution = Solution()
solution.getMostCommonColor(array: colorArray) //This is where I get the error
The way you've defined it, colorArray is an instance property of Solution, meaning you need to access it through some instance of the Solution class.
You've already created an instance down below, so you could do this:
let solution = Solution()
solution.getMostCommonColor(array: solution.colorArray)
However, I suspect you meant for colorArray to be a global variable of sorts (since this is just a playground, after all), so another option is to move colorArray out of the Solution class:
var colorArray = ["Red","blue","green","black","red","blue","yellow","purple","Red","Red","Red","yellow","yellow","black","green","black","purple","black","yellow","purple","purple","blue","yellow","blue","green","green","yellow","pink"]
class Solution {
func getMostCommonColor(array: [String]) -> [String] {
var topColors: [String] = []
var colorDictionary: [String: Int] = [:]
for color in array {
if let count = colorDictionary[color] {
colorDictionary[color] = count + 1
}else{
colorDictionary[color] = 1
}
}
let highestValue = colorDictionary.values.max()
for (color,count) in colorDictionary {
if colorDictionary[color] == highestValue {
topColors.append(color)
}
}
return topColors
}
}
let solution = Solution()
solution.getMostCommonColor(array: colorArray)

Using UISearchbar with JSON to filter the result

I am fetching the data from JSON using http in the following code:
I have an ObjectModel, DownloadModelProtocol, and TableViewController
(Modal.swift)
class OrderItemModal: NSObject {
var deptname: String!
var staffname: String!
var status: String!
var userid: String!
}
(DownloadOrderModal.swift):
protocol OrderDownloadProtocol: class {
func itemsDownload(items: Array<Any>)
}
...
let bmsOrders = NSMutableArray()
...
weak var delegate: OrderDownloadProtocol!
let urlPath = "http://localhost/production/api/db_orders.php"
func downloadItems() {
let url = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
...
for i in 0..<jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let bmsOrder = OrderItemModal()
....
bmsOrders.add(bmsOrder)
....
declaration:
var orderItems = [OrderItemModal]()
var filterArray= [OrderItemModal]()
func itemsDownload(items: Array<Any>) {
orderItems = items as! [OrderItemModal]
}
and viewDidLoad:
let bmsOrder = DownloadOrderModal()
bmsOrder.delegate = self
bmsOrder.downloadItems()
this is the JSON result:
(
{
"deptname" = "Production";
"staffname" = Warehouse;
"status" = 1;
"userid" = ware;
})
This the the search bar code
filterArray = orderItems.filter( { ($0. staffname) (of: searchText, options: .caseInsensitive) })
And finally, this is the error:
Cannot assign value of type '[OrderItemModal]' to type '[String]'
Ultimately, I will populate the data into a table.
You have a few issues. It seems that orderItems is an NSArray array of OrderItemModal values. The first thing you need to do is to stop using NSArray and use a Swift array of the proper type. In this case it should be [OrderItemModal]. You will need to ensure filterArray is also declared as [OrderItemModal].
The result of a filter on such an array will be an array of OrderItemModal but you are attempting to force cast the result as an array of String.
You are also force-casting the closure to be (Any) -> Bool. There's no need for that.
And lastly, you are needlessly using NSString. Stick with String.
All you need is:
filterArray = orderItems.filter { (item) -> Bool in
return item.staffname.range(of: searchText, options: .caseInsensitive) != nil
}
Even simpler:
filterArray = orderItems.filter { $0.staffname.range(of: searchText, options: .caseInsensitive) != nil }

Cannot convert value of type 'String?!' to expected argument type 'Notifications'

I am trying to check the id of a record before I put it into the array, using xcode swift
here is the code. But, i get the following error
Notifications.swift:50:46: Cannot convert value of type 'String?!' to expected argument type 'Notifications'
on this line
*if (readRecordCoreData(result["MessageID"])==false)*
Please can some one help to explain this error
import CoreData
struct Notifications{
var NotifyID = [NSManagedObject]()
let MessageDesc: String
let Messageid: String
init(MessageDesc: String, Messageid:String) {
self.MessageDesc = MessageDesc
self.Messageid = Messageid
// self.MessageDate = MessageDate
}
static func MessagesWithJSON(results: NSArray) -> [Notifications] {
// Create an empty array of Albums to append to from this list
var Notification = [Notifications]()
// Store the results in our table data array
if results.count>0 {
for result in results {
//get fields from json
let Messageid = result["MessageID"] as! String
let MessageDesc = result["MessageDesc"] as? String
let newMessages = Notifications(MessageDesc: MessageDesc!, Messageid:Messageid)
//check with id's from core data
if (readRecordCoreData(result["MessageID"])==false)
{
Notification.append(newMessages)
}
}
}
return Notification
}
//check id
func readRecordCoreData(Jsonid: String) -> Bool {
var idStaus = false
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest(entityName: "ItemLog")
//3
do {
let resultsCD = try! managedContext.executeFetchRequest(fetchRequest)
if (resultsCD.count > 0) {
for i in 0 ..< resultsCD.count {
let match = resultsCD[i] as! NSManagedObject
let id = match.valueForKey("notificationID") as! String
if (Jsonid as String! == id)
{
idStaus = true
}
else{
idStaus = false
}
}
}
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
return idStaus
}
One of your methods is static and the other one is not :
func readRecordCoreData(Jsonid: String) -> Bool
static func MessagesWithJSON(results: NSArray) -> [Notifications]
Depending on what you want to accomplish you could declare both static, none, or replace
//check with id's from core data
if (readRecordCoreData(result["MessageID"])==false)
{
Notification.append(newMessages)
}
By
//check with id's from core data
if (Notifications.readRecordCoreData(Messageid)==false)
{
Notification.append(newMessages)
}
Not sure if the code will work past compilation however as there are many readability issues

Populating a global dictionary from a loop

I'm trying to loop through some parsed JSON from an API and build a global dictionary that can be accessed throughout the app. I'm getting an error when trying to set the dictionary item.
The global var is set as
var propListArray = [Int : [String : String]] ()
//LOOP THROUGH PROPERTIES
let itemArray = dataDictionary["properties"] as! NSArray //HAVE DATA HERE JUST FINE
var i = 0
for item in itemArray {
let propertyID = item["id"]! as! String
print(propertyID) //THIS PRINTS FINE IN CONSOLE
propListArray[i]!["propertyID"] = propertyID //THIS THROWS AN ERROR ON EXECUTION
i++
}
I want to end up with an array like this
propertyListArray[0]["propertyID"] = "16001"
propertyListArray[1]["propertyID"] = "16001"
propertyListArray[2]["propertyID"] = "16001"
propertyListArray[3]["propertyID"] = "16001"
There will be other vars per item as well such as title, etc. But just trying to get at least the one var in on each item.
Any help would be amazing!! Thank you.
propListArray is empty so trying to get the dictionary at any index will return nil, Then when you try to set a value it will cause the error.
You can fix it by creating the dictionary then assigning it to you global dictionary at the correct index.
var propListArray = [Int : [String : String]] ()
let itemArray = dataDictionary["properties"] as! NSArray
for (index, item) in itemArray.enumerate() { {
let propertyID = item["id"]! as! String
let data = ["propertyID": propertyID]
propListArray[index] = data
}
Quick fix:
var propListArray = [Int : [String : String]] ()
let itemArray = dataDictionary["properties"] as! NSArray
var i = 0
for item in itemArray {
let propertyID = item["id"]! as! String
propListArray[i] = ["propertyID": propertyID]
i++
}
However, i++ within the for loop is not idiomatic Swift. In fact, i++ and C-style for loops are going to be removed in Swift 3. It is better to use enumerate():
var propListArray = [Int : [String : String]] ()
let itemArray = dataDictionary["properties"] as! NSArray
for (i, item) in itemArray.enumerate() {
let propertyID = item["id"]! as! String
propListArray[i] = ["propertyID": propertyID]
}
propListArray[0]?["propertyID"]

Swift: Unable to get array values from singleton class

Hi I retrived value from JSON and stored in NSMutableArray. I have tried this like Singleton. I have used empty swift file for this. Datas successfully retrieved and stored in NSMutableArray which is already declared in mainViewController. Then, if I use that NSMutableArray value in mainViewController, it shows empty array.
My coding is below. Kindly guide me.
Empty Swift File
public class json_file{
var prod_Obj = product_mainVC()
class var shared: json_file
{
struct Static
{
static let instance: json_file = json_file()
}
return Static.instance
}
func dataFromJSON()
{
let url = NSURL(string: "http://........--...4900a20659")!
var data : NSData = NSData(contentsOfURL: url, options: NSDataReadingOptions.DataReadingMapped, error: nil)!
var dict: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
let dataArray = dict["data"] as [[String:AnyObject]] // The array of dictionaries
for object in dataArray {
let category_name = object["category_name"] as String
prod_Obj.ct_name_arr.addObject(category_name)
let category_child = object["category_child"] as [[String:AnyObject]]
for child in category_child {
let sub_category_name = child["sub_category_name"] as String
prod_Obj.sub_ct_name_arr.addObject(sub_category_name)
}
}
println(prod_Obj.ct_name_arr) //Here value is Getting
println(prod_Obj.sub_ct_name_arr) //Here value is Getting
}
}
viewDidLoad
{
json_file.shared.dataFromJSON()
println(ct_name_arr) //Prints Empty Array [Intially Declared as NSMutableArray]
println(sub_ct_name_arr) //Prints Empty Array [Intially Declared as NSMutableArray]
}
I was trying understand the problem, but I can't see the product_mainVC. Because this I remake your class with little modifications.
class JsonFile
{
private(set) var categoryNames:[String];
private(set) var subCategoryNames:[String];
class let shared:JsonFile = JsonFile();
private init()
{
self.categoryNames = [];
self.subCategoryNames = [];
}
func dataFromJson()
{
let url = NSURL(string: "http://........--...4900a20659")!
if let data : NSData = NSData(contentsOfURL: url, options: NSDataReadingOptions.DataReadingMapped, error: nil)
{
if let dict: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary
{
if let dataArray = dict["data"] as? [[String:AnyObject]] // The array of dictionaries
{
for object in dataArray {
let category_id = object["category_id"] as Int
let category_name = object["category_name"] as String
categoryNames.append(category_name);
let category_child = object["category_child"] as [[String:AnyObject]]
for child in category_child {
let sub_category_id = child["sub_category_id"] as Int
let sub_category_name = child["sub_category_name"] as String
subCategoryNames.append(sub_category_name);
}
}
}
}
}
println(categoryNames);
println(subCategoryNames);
}
}
I did
Modify your way to do Singleton to a safe and more simple mode, create the arrays categoryNames and subCategoryNames internal in class because this is better to manipulate, and protect your fetch data to safe from possibles crash.
Implementation
JsonFile.shared.dataFromJson();
println("count categoryNames");
println(JsonFile.shared.categoryNames.count);
println("count subCategoryNames");
println(JsonFile.shared.subCategoryNames.count);
You need think about
This code is sync, and because this if you have a big data or request slow, the main thread from your application will freeze waiting return and it is bad for your user. Think if is necessary be sync.
let category_id = object["category_id"] as Int is never used. Why do you stay with this in code?