-3

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)
        )
    ) 
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
emololftw
  • 11
  • 3
  • What do you mean by "transfer whole swift array to objective C file"? Please explain – gmuraleekrishna Dec 22 '17 at 18:14
  • Are you basically asking how to call the objective-c methods from your swift code? – ghostatron Dec 22 '17 at 18:28
  • i have array in swift and wanna call it in objective C – emololftw Dec 22 '17 at 18:46
  • How are you exposing it, where is it stored? Have you imported your Swift header into your Objective-C file as mentioned here? https://stackoverflow.com/questions/24102104/how-to-import-swift-code-to-objective-c We might need to see more code before we can fully understand what the issue is. – Kane Cheshire Dec 22 '17 at 19:02
  • I try it again with importing header file into swift. Thanks for answers! – emololftw Dec 22 '17 at 19:34
  • How is `mainSelectionMonthArrayValues` declared? You probably need to declare it as (or bridge it to) `NSArray`. – i_am_jorf Dec 22 '17 at 19:37
  • Yes. Declaration is: var mainSelectionMonthArrayValues = [String]() ... But transfering into NSArray dont works for me :/ – emololftw Dec 22 '17 at 19:39

1 Answers1

-1

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.

Patru
  • 4,481
  • 2
  • 32
  • 42