Passing custom swift object to javascript methods as input - ios

I'm using javascriptcore framework to interact with the JS file functions.
I have the object in the following type
class Test: NSObject {
var testId: NSNumber?
var testIndex: Int?
var testDate: NSDate?
var title: String?
var status: String?
}
I'm creating the array of object and passing as input to javascript method like below
var testArray = [Test]()
for i in 0 ..< 4 {
let test = Test()
test.testId = i * 3
test.testIndex = i
test.testDate = NSDate()
test.title = "Test \(i)"
test.status = "waiting"
testArray.append(test)
}
I have to pass this array to method to JS, Please let me know how to pass it.

You should be able to pass the attributes to JS as described here:
http://nshipster.com/javascriptcore/
The blocks-way
Couldn't test the code without Mac now, but is maybe working:
let testArrayCount: #convention(block) Void -> String = {
return testArray.count
}
let transferTest: #convention(block) String -> (String, String, String, String, String) = {
i in
return ( String(testArray[i].0),String(testArray[i].1),String(testArray[i].2),testArray[i].3,testArray[i].4 )
}
context.setObject(unsafeBitCast(testArrayCount, AnyObject.self), forKeyedSubscript: "testArrayCount")
context.setObject(unsafeBitCast(transferTest, AnyObject.self), forKeyedSubscript: "transferTest")
Now you can read the array in your JS-code (this is Pseudocode now):
var jsTestArray;
for ( i=0, i<testArrayCount(), i++ ) {
jsTestArray.append( transferTest(i) );
}
After loading object after object into your Javascript, you should be able to work with the array. If you don't want to work with strings only you may have to cast the things back (but as JS noob I don't wheather it's necessary)
Using JSExport Protocol
You can try it also the other way, described on http://nshipster.com/javascriptcore/ .
Hope, this answered your question?

Related

How to update Firebase with struct

I have a struct EventDate and am trying to update a reference in Firebase.
struct EventDate {
var club: String = ""
var date: String = ""
var eventText: String = ""
var free: String = ""
var monthYear: String = ""
}
My update function throws lldb. I guess because the keys are no Strings(?)
func updateEvent(_ backendlessUserObjectID: String, event: EventDate) {
let reference = firebase.child("eventDates").child(backendlessUserObjectID).child(event.date)
reference.setValue(event) { (error, ref) -> Void in
if error != nil {
print("\(error)")
}
} // lldb here
}
If I change the function to the following, everything is fine (because Keys and Values are now Strings?)
func updateEvent(_ backendlessUserObjectID: String, event: EventDate) {
let item: NSMutableDictionary = ["club" : event.club,
"date" : event.date,
"eventText" : event.eventText,
"free" : event.free,
"monthYear" : event.monthYear]
let reference = firebase.child("eventDates").child(backendlessUserObjectID).child(event.date)
reference.setValue(item) { (error, ref) -> Void in
if error != nil {
print("\(error)")
}
}
}
Am I right that I receive lldb because the keys from my models are not Strings? Or what am I missing and how will I be able to save the values into my Firebase using my model without creating the NSMutableDictionary? Help is very appreciated.
PS: print(event.date) = 201610120200000000 -> the desired value for .child
Firebase data exists in a JSON format which can be thought of as key:value pairs. The keys must be strings and the values can be any of the for data types mentioned in Dravidians answer (which is a correct answer and should be accepted). I would like to add some additional comments that may help as well.
There are times when you want to use a structure in code and that can't be written to Firebase directly - you need some way to get the data out of the structure into something Firebase likes - which is a Dictionary.
Heres an example (Swift 3, Firebase 2.x)
struct EventDate {
var club: String = ""
var date: String = ""
var eventText: String = ""
var free: String = ""
var monthYear: String = ""
func getDict() -> [String:String] {
let dict = ["club": self.club,
"date": self.date,
"eventText": self.eventText,
"free": self.free,
"monthYear": self.monthYear
]
return dict
}
}
var event = EventDate()
event.club = "Wimbledon"
event.date = "20161023"
event.eventText = "Special Tennis Event"
event.free = "Free"
event.monthYear = "10/2016"
let ref = self.myRootRef.child(byAppendingPath: "events")!
let eventRef = ref.childByAutoId() //Firebase 2.x
eventRef?.setValue( event.getDict() )
This results in a node being written to Firebase that looks like this
"events" : {
"-KUli8oiM_KKw8GZ0MMm" : {
"club" : "Wimbeldon",
"date" : "20161023",
"eventText" : "Special Tennis Event",
"free" : "Free",
"monthYear" : "10/2016"
}
}
No it has nothing to do with the type of keys that you are trying to save in your Firebase Database its just that struct is a dataModel or to be precise a physically grouped list of variables which you initialise with some custom Data, and you can only save four types of values types in your Firebase Database:-
NSDictionary
NSArray
NSNumber
NSString
Look up the docs :- Read And Write data, Firebase- iOS
So when you cast your values in a NSMutableDictionary, you come clean of struct. And struct and class is not recognisable by the Firebase Database.

Dictionary inside dictionary

I am trying to use a list that is a value for a dictionary key/pair set, and this dictionary is itself a value in a key/pair set in a dictionary. To explain, this is how I initialize it.
var dictOfEvents = [Int: [Int: [PFObject]]]()
I am trying to add events to the list, with the inner dictionary's key being the day of month and the outer one being the month. For example, an event on May 1 would be:
dictOfEvents[5:[1:[ListOfEvents]]
Where ListOfEvents is an array of PFObjects. Before I added the month functionality, and thus the outer dictionary, the way I added new events was:
` self.dictOfEvents[components.day] = [event]
But now, when I try to extend this with:
self.dictOfEvents[components.month]?[components.day]! = [event]
It does not work. Any explanation on how to create new event lists and access this double layer dictionary would be greatly appreciated.
(Note: I don't know where to put the ! and the ? in the last piece of code so please excuse me if I made a mistake.)
Here is what I think could be a good use of optionals in your case (and should respond to your question):
var dic: [Int: [Int: [String]]] = [:]
dic[5] = [1:["Hello", "World"]]
if let list = dic[5]?[1] {
// your list exist and you can safely use it
for item in list {
println(item)
}
}
I just used String instead of PFObject.
A different approach could be:
/*
Define a struct to encapsulate your Month and Day
Make it Hashable so that you can use it as Dictionary key
*/
public struct MonthDay: Hashable {
let month: Int
let day: Int
public var hashValue: Int { return month * 100 + day }
}
public func ==(lhs: MonthDay, rhs: MonthDay) -> Bool {
return lhs.month == rhs.month && lhs.day == rhs.day
}
var dictOfEvents = [MonthDay :[String]]()
let aMonthAndDay = MonthDay(month: 5, day: 1)
dictOfEvents[aMonthAndDay] = ["Hello", "World"]
if let list = dictOfEvents[aMonthAndDay] {
// your list exist and you can safely use it
for item in list {
println(item)
}
}
U can simple change:
self.dictOfEvents[components.month]?[components.day]! = [event]
to :
self.dictOfEvents[components.month]![components.day]! = [event]
Because Dictionary has subscript, Dictionary? doesn't have subscript.
if U try add Events to Dictionary. I suggest to use this:
var dictOfEvents = [Int: [Int: [PFObject]]]()
var dictOfDayEvents = [Int:[PFObject]]()
dictOfDayEvents.updateValue([PFObject()], forKey: 1)
dictOfEvents.updateValue(dictOfDayEvents, forKey: 5)

Cannot Invoke Filter With Argument List '((_) -> _)' [already implemented equatable]

I am trying to use filter on an array, but I keep getting this error. I checked the earlier answer, but I have already implemented "equatable" on my object.
Btw, what does this error mean anyway?
// trying to use filter
var distance_array = [
FilterOption(title: "0.5 miles", value:500, currentSetting: false)...]
var filtered_distance: [FilterOption]!
filtered_distance = distance_array.filter({ ($0.currentValue == true) })
// FilterOption Class
class FilterOption: NSObject, Equatable {
var title:String!
var value: AnyObject?
var currentSetting: Bool!
init(title:String, value:AnyObject?, currentSetting:Bool){
self.title = title
self.value = value
self.currentSetting = currentSetting
}
class func convertDictionaryToFilterOption(dict:Dictionary<String, String>) -> FilterOption{
return FilterOption(title:dict["name"]!, value:dict["code"]!, currentSetting: false)
}
}
func == (lhs: FilterOption, rhs: FilterOption) -> Bool {
var title = (lhs.title == rhs.title)
var setting = (lhs.currentSetting! == rhs.currentSetting!)
return title && setting
}
That compiler error is a little misleading. If you were really testing to see if one FilterOption was equal to another (a common problem that rears its head when comparing objects in filter closure), then you'd have to make it conform to Equatable. But that's not the problem here.
In this case the problem is simply that the code refers to currentValue, rather than currentSetting. If you fix that, the compiler error goes away.
By the way, you can simplify that filter statement:
let filteredDistance = distanceArray.filter { $0.currentSetting }
The currentSetting is already a Bool, so if you're testing to see if it's true, you can just return it directly. There were also some extraneous parentheses.

get the array index in swift [duplicate]

This question already has answers here:
How to find index of list item in Swift?
(23 answers)
Closed 7 years ago.
Here is my code in Swift for an iOS app.
class Test {
var name: String
var sname: String
init(name: String, sname: String) {
self.name = name
self.sname = sname
}
}
class ArrTest {
var arr = [Test]()
init() {
arr.append(Test(name: "test1", sname: "surname1"))
arr.append(Test(name: "test2", sname: "surname2"))
arr.append(Test(name: "test3", sname: "surname3"))
arr.append(Test(name: "test4", sname: "surname4"))
}
}
var x = ArrTest()
let obj = x.arr.filter { $0.name == "test1" }.first
println(obj?.sname)
I want to get the index and not the object of the first array object (in the array var). The "obj" x.arr.filter... returns the first object. I need the index of the correct object.
Well you can simply use the method find of the array:
// This is pseudocode haven't tried it but should go along those lines.
var x = ArrTest()
var searchMe = Test("test1", "surname1");
let obj = find(x, searchMe);
For this to work you need to implement the Equatable protocol, here you can find a simple tutorial. After that find should work with your test class.
let arr:Array = ["a","b","c"]
find(arr, "c")! // 2
find(arr, "d")

Find Object with Property in Array

is there a possibility to get an object from an array with an specific property? Or do i need to loop trough all objects in my array and check if an property is the specific i was looking for?
edit: Thanks for given me into the correct direction, but i have a problem to convert this.
// edit again: A ok, and if there is only one specific result? Is this also a possible method do to that?
let imageUUID = sender.imageUUID
let questionImageObjects = self.formImages[currentSelectedQuestion.qIndex] as [Images]!
// this is working
//var imageObject:Images!
/*
for (index, image) in enumerate(questionImageObjects) {
if(image.imageUUID == imageUUID) {
imageObject = image
}
}
*/
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
var imageObject = questionImageObjects.filter( { return $0.imageUUID == imageUUID } )
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
You have no way to prove at compile-time that there is only one possible result on an array. What you're actually asking for is the first matching result. The easiest (though not the fastest) is to just take the first element of the result of filter:
let imageObject = questionImageObjects.filter{ $0.imageUUID == imageUUID }.first
imageObject will now be an optional of course, since it's possible that nothing matches.
If searching the whole array is time consuming, of course you can easily create a firstMatching function that will return the (optional) first element matching the closure, but for short arrays this is fine and simple.
As charles notes, in Swift 3 this is built in:
questionImageObjects.first(where: { $0.imageUUID == imageUUID })
Edit 2016-05-05: Swift 3 will include first(where:).
In Swift 2, you can use indexOf to find the index of the first array element that matches a predicate.
let index = questionImageObjects.indexOf({$0.imageUUID == imageUUID})
This is bit faster compared to filter since it will stop after the first match. (Alternatively, you could use a lazy sequence.)
However, it's a bit annoying that you can only get the index and not the object itself. I use the following extension for convenience:
extension CollectionType {
func find(#noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
return try indexOf(predicate).map({self[$0]})
}
}
Then the following works:
questionImageObjects.find({$0.imageUUID == imageUUID})
Yes, you can use the filter method which takes a closure where you can set your logical expression.
Example:
struct User {
var firstName: String?
var lastName: String?
}
let users = [User(firstName: "John", lastName: "Doe"), User(firstName: "Bill", lastName: "Clinton"), User(firstName: "John", lastName: "Travolta")];
let johns = users.filter( { return $0.firstName == "John" } )
Note that filter returns an array containing all items satisfying the logical expression.
More info in the Library Reference
Here is a working example in Swift 5
class Point{
var x:Int
var y:Int
init(x:Int, y:Int){
self.x = x
self.y = y
}
}
var p1 = Point(x:1, y:2)
var p2 = Point(x:2, y:3)
var p3 = Point(x:1, y:4)
var points = [p1, p2, p3]
// Find the first object with given property
// In this case, firstMatchingPoint becomes p1
let firstMatchingPoint = points.first{$0.x == 1}
// Find all objects with given property
// In this case, allMatchingPoints becomes [p1, p3]
let allMatchingPoints = points.filter{$0.x == 1}
Reference:
Trailing Closure
Here is other way to fetch particular object by using object property to search an object in array.
if arrayTicketsListing.contains({ $0.status_id == "2" }) {
let ticketStatusObj: TicketsStatusList = arrayTicketsListing[arrayTicketsListing.indexOf({ $0.status_id == "2" })!]
print(ticketStatusObj.status_name)
}
Whereas, my arrayTicketsListing is [TicketsStatusList] contains objects of TicketsStatusList class.
// TicketsStatusList class
class TicketsStatusList {
internal var status_id: String
internal var status_name: String
init(){
status_id = ""
status_name = ""
}
}

Resources