How to decode rootObject - arrays

I have a custom class and I am trying to save an array within my class. In the encodeWithCoder method I use the encodeRootObject:theShotArray to save the data. What should I use for the initWithCoder method. There doesn't seem to be a decodeRootObject method. Is there a better way to save an array.

NSArray conforms to the NSCoding protocol, so you can say:
NSArray *rootArray = [[NSArray alloc] initWithCoder:myKeyedUnarchiver];
It may be a little confusing that NSArray's reference page doesn't specifically mention -initWithCoder:, but that's because that method is already described in the NSCoding protocol reference, and the NSArray reference page specifies NSCoding.

Related

The api returns both an object and an array

I'm new to json, there was a problem and I couldn't find a solution
I was given an api and when executing a get request, I get some object, but if there is no data in the object, an array is returned.
At the moment I was able to get Any?, instead of JSONArray or JSONObject, but there was a problem with converting Any? to the class
How to convert data to kotlin data class correctly?
returned object
returned array
The class I'm converting the json request to:
data class ProductInfo (var product:Product?,var specifications: JsonObject?,var supplements: Any?,var files:List<File>?,var feedback: Feedback?)
This seems something that the backend has to solve for you. They can give you a nullable array or just an empty array, whatever is more convenient, but implementing polymorphism is not something trivial.
Jackson makes polymorphism easier than Gson, however, it is always required some kind of anchor to know how to route the parsing, in this case, you don't have any.
Jackson uses an annotation and there you have indicate in which thing is going to pivot:
#JsonSubTypes.Type(value = Nothing.class, name = "????")
With Gson you have to implement your own JsonDeserializer but again, how do you know what type is it? If it can be cast to array then is nothing? Just writing that seems like an antipattern.

Query objects where the pointer object matches any objects within a PFObject array

My class Posts has a column that is a pointer to another class called Styles. Each post must be associated to a Style object as a rule of thumb.
My problem: I can't get only the posts that are associated to one or more styles.
My object selectedStyles, that is an array of PFObjects that already contains the style objects I would like to use to match the query. So populating the selectedStyles is not an issue, but how to use it to produce my query is.
What I am doing at the moment is:
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Posts")
query.whereKey("bellongsToStyle", containsAllObjectsInArray: [selectedStyles])
query.orderByDescending("createdAt")
return query
If I could translate the whereKey method in plain english I would say:
...contains - ANY OF THE - PFObjectsInArray [selectedStyles]
I am not so sure if that is possible... any ideas?!
I think I am too late for this but you can just add
[IncludeKey:"bellongsToStyle"]
when you are querying in your 'Posts' class
don't need to run any extra query for that
here's a small reference
http://blog.parse.com/announcements/queries-for-relational-data/

Check if an array exists Swift

In Objective-C I would write:
#property (nonatomic, strong) NSMutableArray *privateArray;
if (!self.privateArray) {
// Populate the privateArray
}
Which checks that an array does not already exists before populating it. How could this be achieved in Swift?
I have tried implementing it by translating my Obj-C code but am given an Xcode error "Unary operator '!' can not be applied to an operand of type '[AnyObject]'"
Swift attempt:
private var privateArray: [AnyObject]?
if !privateArray {
// Populate the array
}
How would I check if the array exists before populating the array, in Swift?
Is it just as simple to use .isEmpty on the array?
Thanks in advance!
In Objective-C this is called lazy loading. You declare a property but only create the property the first time it is accessed.
The trap you have fallen into is thinking that Swift is just a different syntax around Objective-C.
In Swift you can have a lazy var...
You can declare it like this...
#lazy var someArray: [String] = {
return ["Hello", "World"]
}
This will do what you want.
As a side note. It's generally not a good idea to use AnyObject especially if you know the type of object the array will contain.
Also, by using this method you don't need the array to be optional and so can avoid the whole unwrapping thing.
You can read more about lazy properties by googling it. I found this and it looks quite good... http://mikebuss.com/2014/06/22/lazy-initialization-swift/
You should write:
if (privateArray == nil) {
// Populate the array
}

Keep arrays between views

I have a main view where I initialize severals arrays and released them in viewDidUnload. So when I change to other view where I have the instruccions and I come back to the main view all the array are nil again.
What I what to do is: when the app is lunched, the arrays are initialized, y use them, I could go to others views and when come back to the main view that the arrays keep the values, and only when the app is close then release all the arrays.
How do I have to do it?
Just release them in -dealloc and initialize them in viewDidLoad. That way theyre only released or nullified when the view shuts down and when it is reloaded, they are reinitialized.
Hope this helps.
You can use different approaches for your problem. I can tell you some ways.
You can write methods for view initialization which will take NSArray as parameter. Like:
- (id)initWithArray:(NSArray *)myArray {
[super init];
//here you can store an NSArray inside this ViewController in #property. For ex:
currentArray = myArray;
return self;
}
You can use global storage like CoreData and use NSManagedObject class to define instances for necessary object in every view.
You can use delegate approach. For example you can delegate methods from second view in first view.

How to #synthesize a C-Style array of pointers?

I have a property defined in a class like so:
#interface myClass
UIImageView *drawImage[4];
...
#property (nonatomic, retain) UIImageView **drawImage;
...
#synthesize drawImage; // This fails to compile
I have found similar questions on StackOverflow and elsewhere, but none that really address this issue. What is the most Objective-C kosher way to do this?
You can't; you have to change that retain to assign and handle memory management yourself; as you can't send -[retain] to an UIImageView ** (as it isn't an object.)
That is to say; you can't actually use #synthesize as that would lead to epic memory leaks with assign and doesn't work with retain or copy; what you need to do is to implement -drawImage and -setDrawImage: and write some actually accessor code.
But don't do that. Use an NSArray.
The most "kosher" way would be to create an NSArray of UIImageView objects, instead of a C-style array.
A problem you'll encounter is that functions can't return C-style arrays in C or Objective-C, and another problem you might face is that you can't assign a pointer type to an array type (which is what your synthesised setter may be trying to do). There are at least two solutions:
Use an NSArray rather than a C-style array. This is the cleanest route, especially since you only have to manage the memory of one object rather than four.
Wrap the C-style array into a struct. You can't return C-style arrays straight from functions but you can return a struct that has an array member (be aware that there is no retain/release going on).
typedef struct {
UIImage *image[4];
} ImageCollection;
...
#property (nonatomic, assign) ImageCollection drawImage;
...
#synthesize drawImage;

Resources