mack mack - 6 days ago 4
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

menu.option
?

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")
return
}

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")

break
}

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.append(country)
}

countryData.sortInPlace { $1.name > $0.name }
self.reloadAllComponents()

} 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

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() {
        super.viewDidLoad()

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