mack mack - 1 year ago 63
iOS Question

How to call the nsobject model class to menu option?

Here I have use this library file for create the drop down option from uiview. Here is the library I am using Drop down menu library. But in that we need to call `menu1.option = [1,2,3,4]1. But in my own code, I have in seperate json flie.

My requirement is, still now I am showing the list of counties in uipicker. But I need to show them in drop down option. How can I call that json to


My code :

menu1.options = ["1","2","2","3","3","4","5"]

menu1.editable = false
menu1.delegate = self

my previous uipicker to show the list of countries is like :

if textField == country {
ctCountryPicker.showPicker(self, inputText: textField)

my json code loding :

Loads content from .json file
private func loadData() {
let bundlePath = NSBundle(forClass: CountryPicker.self).pathForResource("SwiftCountryPicker", ofType: "bundle")

if let path = NSBundle(path: bundlePath!)!.pathForResource("EmojiCountryCodes", ofType: "json")

do {
let jsonData = try NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe)
let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments)

var countryCode: String?

if let local = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String {
countryCode = local

guard let countries = json as? NSArray else {
print("countries is not an array")

for subJson in countries{

guard let name = subJson["name"] as? String, iso = subJson["code"] as? String, emoji = subJson["emoji"] as? String else {

print("couldn't parse json")


let country = Country(name: name, iso: iso, emoji: emoji)

// set current country if it's the local countr y
if country.iso == countryCode {
pickedCountry = country

// append country

countryData.sortInPlace { $ > $ }

} catch {
print("error reading file")



Now by using
menu1.option =
, how can I call my JSON file to show the list of option in my drop down?

Answer Source

create one category file Category_country_codes and create two funcation.

func get_Country(arr : NSArray) -> NSArray {
        let arr : NSArray = arr.valueForKey("Country") as! NSArray
        return arr
func get_state(arr : NSArray , index : NSString) -> NSArray {
    let arr1 = arr;
    let namePredicate = NSPredicate(format: "Country like %@",index);

    let filteredUserData  = arr1.filteredArrayUsingPredicate(namePredicate)
    print("names = ,\(filteredUserData)");

    return filteredUserData

class UserInfoViewController: UIViewController

var countryname : NSArray = []
var arrayOfItems : NSArray = NSArray()
let category_country = Category_country_codes()

override func viewDidLoad() {

        let chapterPath = NSBundle.mainBundle().pathForResource("country-states", ofType: "plist")
        arrayOfItems = NSArray(contentsOfFile: chapterPath!)!
        menu1.options = self.category_country.get_Country(self.arrayOfItems) as! Array<String>
        let set = NSSet(array: menu1.options)
        menu1.options = NSArray(array: set.allObjects as! Array<String>) as! Array<String>

        menu1.editable = false
        menu1.delegate = self


  func dropDownMenu(menu: ZHDropDownMenu!, didChoose index: Int) {
      print("\(menu) choosed at index \(index)")
      let countryname : NSString = menu1.options .objectAtIndex(index) as! NSString
      let statedata = self.category_country.get_state(self.arrayOfItems, index: countryname)
      print(statedata .valueForKey("State")) // here set your second menu state . like 
      menu2.option = statedata .valueForKey("State")

In ZHDropDownMenu.swfit file made one changes that dont forgot this

 public var options:NSArray = []

Reading my whole answer and update your code. dont forgot to approve it . Happy Coding.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download