Using Swift 2 and Parse 1.9.1, I cannot seem to find the fetchAll() function. Working with a single PFObject, the following seems to work where object is a PFObject:
object.fetchInBackground()
However when trying to fetch an array of PFObjects with fetchAllInBackground(), which the documentation seems to allow, the code completion tool does not find any such method. Has the function changed names and if so, how can I use it? As a note, the way that I am trying to use the method is as follows, where listOfObjects is an array of PFObjects:
listOfObjects.fetchAllInBackground()
If you read the documentation you will see that fetchAllInBackground and the related methods are class methods on PFObject, they are not functions of the array class.
You would call it as PFObject.fetchAllInBackground(listOfObjects)
fetchAlInBackground: is a class method.
PFObject.fetchAllInBackground(listOfObjects)
Related
I have problem and i cannot fix it by using google.
I have string array (written in swift). But i have objectiveC files with chart functions.
I need transfer whole swift array into objectiveC file.
Example of array:
for i in 0...11 {
mainSelectionMonthArrayValues.append(
String(WatchlistViewController().fetchDataMonth(
type:1, month: i+1, year: 2017)
)
)
}
Given you are not very specific about the code that "does not work" or the specific error you get it is hard to answer anything precisely. But given what Google finds in books for "cocoa pass swift string array to objective-c" you will have
To call an NSArray function on a Swift array you may have to cast to NSArray
so it is a pretty safe bet you will have to do this too if you want to pass mainSelectionMonthArrayValues to an Objective-C method (with an appropriate interface). Casting to an Objective-C class will not be free in most cases, but it is likely to be a constant time operation. Note however, that you will have to coerce if your object needs to be mutable on the Objective-C end. So try passing your array using something like
objCRef.callObjCMethod(mainSelectionMonthArrayValues as NSArray)
If this again "does not work" then you should provide us with more info on the kind of error you experience.
In Objective C I have previously been able to implement an automatic mechanism to allow an object with a private NSDictionary property to implement a simple protocol by automatically converting method invocations to dictionary 'valueForKey' requests, and passing these to the dictionary that has the appropriate key:value.
For example, I would have an object 'ABCArtist', which would implement the artistProtocol. This protocol has a method -(NSString*)artistName; and the 'ABCArtist' object would implement this method by returning [self.privateDictionary valueForKey:artistName];
This process relied on overriding the NSObject methods:
- (void)forwardInvocation:(NSInvocation *)invocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;
- (id)forwardingTargetForSelector:(SEL)aSelector;
I have tried to apply the same process in Swift, however I am unable to use this as I get the compiler error:
'NSInvocation' is unavailable in Swift: NSInvocation and related APIs
not available
Is there any way that anyone has found to implement a protocol by automatically querying a dictionary for a value with the same name as the protocol method?
The use case for this is in mapping to a dictionary of JSON content returned from an API, if this automatic mapping can be accomplished, I only need to write the protocol, and forward it to the generated JSON dictionary.
That kind of dynamic handling of arbitrary messages at runtime can only be done in Objective-C. For one thing, calls to pure Swift methods don't even go through the Objective-C dynamic dispatch mechanism. So it would have to be an #objc method, and the forwardInvocation: logic will have to be implemented in Objective-C.
I am trying to write a registry in Swift that maps from API's (Protocols) to Implementations (Classes). I would like to be able to provide the registry an API and receive back an instance of the class that implements it. In Objective-C this was fairly trivial - just call NSStringFromProtocol on the protocol and then use that as a key for a dictionary containing the classes that implement them. In Swift, however, we do not have this introspective capability. When I try to do the same I am told that MyAPI.protocol does not have a member "mirrorType". My question to you is how, in Swift, without using #objc protocols, I can map from a protocol itself to the class that implements it. Thanks!
By now it's not possible without using #objc. The solution I've found in this case is using the protocol name (string) as a key for the dictionary for this implementations (In my case I'll always have only one instance per Protocol).
Using #objc will force you to have all your implementations returning AnyObject the equivalent (id) in objective-C (if your function does not return a native objective-C type).
Hope that helps.
I don't want to subclass from NSDictionary but I want to get the literal syntax. It means that I am about to save and retrieve the key by using the "[]". I get this question from Parse iOS SDK, the PFObject is not inherited from the NSMutableDictionary. And they are able to use the dictionary literal syntax.
Here is the link from Parse iOS SDK: https://parse.com/docs/ios_guide#objects/iOS.
See for example the good Object Subscripting article at NSHipster. You just need to implement two methods in your class:
- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
this is a very hard question to ask because I don't want to flood you with all of my code, being that most of it is not pertinent to the problem. So I won't be to surprised if this goes unanswered. Although, it could be something hella simple that I am missing haha. Here it goes:
So my app is storing an array via [encoder] in my appDelegate. The app is full of objects that are creates in a separate NSObject class. Think of it this way for examples sake:
I have my main viewController class. And in appDelegate I define/encode an array of objects.
Now in my main, I fill the array with 10 "cars". My car class has variables such as color, make, model, etc.. Now when I save and relaunch the app, the array that I have saved is now an array containing 10 elements, but it seems to have forgotten all of the variables for each instance of the car class.
When I relaunch the app, If I call NSLog(#"%#",array in appDelegate); It prints 10 lines that look a lot like this:
""
So I know the array is being stored with 10 elements, and 10 elements are saved, but like i said, all of the variables are gone.
Note: On the first run of the app, and the array is being filled for the first time, I can access all the variables perfectly, and nothing is wrong.
Thank you for any help that I can get!!
We need to see the code for your implementation of initWithCoder and encodeWithCoder on the "car" class. If you haven't implemented them, that's your problem.
What is probably happening currently is that only the superclass implementation of these methods is being invoked. This means the correct class will be recreated but no data will be saved or restored.
NSCoding protocol reference doc.
Your main class as well as all the objects you put into the array all need to conform to NSCoding. If the objects in the array aren't NSCoding compliant, they won't get coded automatically.