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;
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.
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)
I'm new here so please help me figure this out. I wonder why does Swift have both NSDictionary and Dictionary classes? My question applies also to the other standard classes like String/NSString, Array/NSArray etc. I'm asking this because i have problem in my code, having to do a lot of casts.
For example, i found the method to load a plist file into a dictionary only in NSDictionary contentsOfFile and not in the native Dictionary class, the latter being the 'advertised' way to go with Swift.
Is this an implementation problem (Swift being new and all) or are there more, deeper, reasons for having the old Objective-C classes? What are the differences?
Both types are there in order to use Objective-C code in swift. Swift array and dictionary are generic type and can hold non-class instances (enum, struct), while NSArray/NSDictonary can only hold NSObject.
In order to simplify your question lets take an example of NSString
NSString is class for a long time in iOS development, it is the predecessor of String
String is the new Swift specific string class where as NSString is the older objective C version and there is a way to convert or bridge between the two,
As per Apple “The entire NSString API is available to call on any String value you create”
now why you want to do this because String class does’t necessarily have all the methods that is on NSString at the moment, so if you want to use method of NSString you can convert a String to NSString and take advantage of those methods
for example
// Double conversion
var strCon = Double(("3.14" as NSString).doubleValue)
// Contains String
var stack = "Stack overflow"
(stack as NSString).containsString("Stack")
// Substring
var overflow = (stack as NSString).substringToIndex(8)
In short if you are trying to do something and there doesn’t seem to be method available on String I recommend to take a look at NSString as there are tons of methods there and chances are it will help you out
same is the case with NSDictionary, NSArray and so on…
I hope it helps
I am trying to learn iOS, I come from Java background where we can have lists/arrays of specific classes like
List<String> l = new ArrayList<>();
List<MyClass> l = new ArrayList<>();
Looking at Objective-C, I can make use of NSArray class to make immutable arrays, but how do I specify that this NSArrray is strictly of type MyClass?
Now Xcode 7 supports some kind of generics for standard collections(e.g. NSArrays). So you can make an array and provide kind of storing objects like this:
NSArray<NSString*> *myArrayOfStrings;
As far as I know, there isn't a built-in mechanism for specifying the type of objects put into NSArrays in Objective-C, but it looks like Swift does what you're looking for if that helps at all:
https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-XID_172
On a slightly related note, there's a decent write-up here about enforcing inserting objects of only a certain type into a subclassed NSMutableArray and throwing exceptions if trying to insert the wrong object types:
NSMutableArray - force the array to hold specific object type only
Sadly there are no generics in Objective-C.
Sorry, Objective-C noob here.
in java doing this is very simple. The code is just...
if(ParseObject.has("value"){
//dosomething
}
In objective-c there doesn't seem to be an equivilant to this method.
To get the values out of the PFObject, you can use either the objectForKey: method or the [] subscripting operator.
If there is no object for a given key, you will get nil.