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.
Related
I create an instance of a Trie object using third party library https://github.com/fpg1503/Aho-Corasick-Swift. Example code to build Trie object is like this:
let trie = Trie.builder()
.add(keyword: "hers")
.add(keyword: "his")
.add(keyword: "she")
.add(keyword: "he")
.build()
However, in my case number of keywords is around 300 thousand, which takes large amount of build this Trie object. My objective is build the trie object just once outside app and save it to use by App for inference on any text. Something like this:
let emits = trie.parse(text: "any text")
However, I am struggling to save this Trie object in swift. In Java (for android), I achieved this task so easily but in Swift it looks very hard thing to do. I am trying hard to achieve this using Codable but that requires all the classes starting from beginning to conform codable. I tried to copy all the codes of (https://github.com/fpg1503/Aho-Corasick-Swift) and change all classes to conform codable. My project now builds successfully but gives runtime error for encoding this Trie object:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee0005ff8)
How can I achieve this in swift?
The problem appears to be that State is recursive, so when you encode a State you get an infinite recursion until you generate a stack overflow.
I'm trying to understand NSLog and how to print to the console in Xcode. I understand that NSLog uses what are called "tokens" to setup the type of variable being referenced to print (I think that's right?). What I need to know is the difference in which tokens to use and what they mean?
For example, after declaring an NSArray like below, I'd want to print the drink names to the console. I could do that like:
NSArray *drinks = #[#"juice", #"water", #"coffee"];
for (NSString *drinkName in drinks) {
NSLog(#"%#", drinkName);
}
So...am I using the #"%#" token because it's an NSString?
I would use #"%i" for an integer, and a #"%f" for a float? What about doubles? If anyone could shed some easy-to-understand beginner's knowledge on NSLog, that would be great! =)
As I understand it, NSLog isn't an Objective C function but a C function built into the foundation of Cocoa. Therefore it conforms to basic C functions with variadic arguments. You can use %# for all objects including NSString. This will in turn call the objects description method and print the appropriate string. Most objects have a rather useful representation already there (e.g. NSArray objects return the descriptions of all their contents)
I have an array of Strings in Swift declared like this:
var DataStreamBuffer : [String] = {return []}()
Appending a new element in Swift, it's easy. I just do:
DataStreamBuffer.append(new_string)
However, I need to append a new element to DataStreamBuffer from a C file. What should be the right procedure in my C code? And what should I do in Swift so DataStreamBuffer is visible from C (do I have to declare anything in the Bridging-Header.h?)
If you by any chance mean Objective-C, then this might help:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122
I haven't played with it myself yet, so I'm not sure if any Objective-C code can call Swift. If that is the case, then any C code in your project can call your Swift code, because any C code is valid Objective-C (i.e. Objective-C is a superset of C). The reverse is not true, however.
Another approach is to have a C function that returns a string that you want to append to the Swift string array. The C function is called from Swift code, which gets the string and actually appends it to the Swift array. You would use a bridging header to make the C function visible to Swift.
A variation of this approach would be to declare a callback function in your C code, import the declaration into Swift using a bridging header, and implement the callback in Swift. This is a more difficult approach, but would come in handy if this part of the app logic is controlled from the C code.
If you provide a more detailed context of what you are doing, people might give you more specific advice or even some examples.
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.