it's been a long time I don't use Swift.
I have a response data like this and I saved it into an array named responseData:
[
{
"type": "Switch",
"name": "Switch1",
"search_key": "is_promotion",
"key": "is_promotion=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch2",
"search_key": "shop_type",
"key": "shop_type=2",
"icon": ""
},
{
"type": "Switch",
"name": "Switch3",
"search_key": "is_certified",
"key": "is_certified=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch4",
"search_key": "shop_free_shipping",
"key": "shop_free_shipping=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch5",
"search_key": "is_loyalty",
"key": "is_loyalty=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch6",
"search_key": "is_using_instant",
"key": "is_using_instant=1",
"icon": ""
},
{
"type": "Switch",
"name": "Switch7",
"search_key": "is_installment",
"key": "is_installment=1",
"icon": ""
},
{
"type": "Range",
"name": "Price Range",
"search_key": "level_Price_Max_Min",
"value": [
{
"option_id": 0,
"option_name": 0
},
{
"option_id": 0,
"option_name": 10000000
}
]
},
{
"type": "ColorTerm",
"name": "Color",
"search_key": "color_key",
"value": [
{
"option_id": 605,
"value": "Black",
"color_id": 13,
"image": "",
"option_name": "Black",
"background": "#000000",
"option_active": "",
"facet_count": 52655
},
Now I wanna group all dictionary with type Switch into one array and I can access to the keys inside it, then present data of both Switch type array and the others type on a UITableView which have 2 section (Switch type in section 0). How can I do it? I have to search some other solution but I don't understand how to apply them to my code for work.
Here is my FilterModel class:
class FilterModel: NSObject, NSCoding, NSCopying {
override func copy(with zone: NSZone? = nil) -> Any {
// This is the reason why `init(_ model: GameModel)`
// must be required, because `GameModel` is not `final`.
let copy = FilterModel(dict: self.dictionary)
if let arrAttribute = NSArray(array: self.value , copyItems: true) as? [AttributeValueModel] {
copy.value = arrAttribute
}
return copy
}
override init(dict: Dictionary<String, Any>) {
super.init(dict: dict);
value = self.valueParse()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var name: String? {
return self.dictionary.getString(forKey: "name")
}
var icon: String? {
return self.dictionary.getString(forKey: "icon")
}
var search_key: String? {
return self.dictionary.getString(forKey: "search_key")
}
var key: String? {
return self.dictionary.getString(forKey: "key")
}
var type: FilterDisplayType {
let type = self.dictionary.getString(forKey: "type")
return self.getType(string: type)
}
var value: [AttributeValueModel] = [];
func valueParse()-> [AttributeValueModel] {
// with switch type, Just set true or false
// Change ParentValue to Child
if type == .Switch {
let dict:Dictionary<String, AnyObject> = [
"option_id": "false" as AnyObject,
"option_name": self.name! as AnyObject,
"name": self.name! as AnyObject,
"icon": self.icon! as AnyObject
]
let item = AttributeValueModel(dict:dict);
return [item]
}
guard let childs = (self.dictionary["value"]) as? [Dictionary<String, AnyObject>]
else { return [] }
var output: [AttributeValueModel] = [];
for aDict in childs {
let item = AttributeValueModel(dict:aDict);
if type == .Range && item.option_id == "0" {
item.setRangeOptionID(aValue: item.option_name!)
}
output.append(item);
}
return output;
}
///get list AttributeValueModel Selected
func selectedValues() -> [AttributeValueModel] {
var output: [AttributeValueModel] = [];
for itemTemp in self.value {
if(itemTemp.selected){
if type == .Switch {
itemTemp.setSelectedOptionID()
}
output.append(itemTemp);
}
}
return output;
}
/// make a Filter Item from attributeValues Seleted
func getFilterItem() -> FilterItem? {
var itemFilter: FilterItem = FilterItem(key: self.search_key!, value: "")
itemFilter.key = self.search_key!
let output: NSMutableArray = [];
for attItem in self.selectedValues() {
if attItem.option_id != "" {
output.add(attItem.option_id!);
}
}
if(output.count == 0) {
return nil;
}
let valueString = output.componentsJoined(by: ",");
itemFilter.value = valueString;
return itemFilter
}
///get list AttributeValueModel Selected
func resetToDefault() -> [AttributeValueModel] {
var output: [AttributeValueModel] = [];
for itemTemp in self.value {
if(itemTemp.selected){
itemTemp.selected = false
if type == .Switch {
itemTemp.setSelectedOptionID()
}
if type == .Range {
itemTemp.setRangeOptionID(aValue: itemTemp.option_name!)
}
output.append(itemTemp);
}
}
return output;
}
//for UI
var wasExpanding = false
var numberOfRow:Int = 0
/************/
var attributeNameLength: Int {
var string = ""
for item in valueParse() {
string += item.option_name!
}
return string.count
}
var lenghtSizeName:Int {
var row:Int = 1
var width:CGFloat = 0
let padding:CGFloat = 8
let screen = screenWidth - 50 - 16
for item in valueParse() {
let size = ((item.option_name ?? "") as NSString).size(withAttributes: [
NSAttributedStringKey.font : UIFont.fontRegular_Big()
])
let totalWidth = size.width + padding + 16
if totalWidth <= CGFloat(32) {
width += 32
if width >= screen {
row += 1
width = 32
}
} else {
width += totalWidth
if width >= screen {
row += 1
width = totalWidth
}
}
}
return row
}
}
You can filter your response data to get only switches in an array.
responseData.filter {($0.type ?? "") == "Switch"}
And of course != would give you non switches.
Setting up a fivem server and trying to get esx_policejobs to work after putting in custom job_grades. Have gotten an error and have been stuck for hours, I don't know if I'm missing something here or not?
Theres also a console line that says stack traceback to a different .lua file, so could that end up being the problem in there instead of the actual config file?
Have looked over and over the config and honestly cant find the problem
recruit = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 1500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 80 },
},
private = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
private-first-class = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 }
},
sergeant = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
lieutenant = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
captain = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
inspector = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
deputy-chief = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
assistant-chief = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
chief-of-police = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
}
Shouldnt be any config errors
Error in console:
Failed to load script config.lua.
Error loading script server/main.lua in resource esx_policejob: #esx_policejob/server/main.lua:5: attempt to index a nil value (global 'Config')
stack traceback:
#esx_policejob/server/main.lua:5: in main chunk```
The problem is with the dashes in the table key names. The manual does give a hint about the form of the table constructor:
A field of the form name = exp is equivalent to ["name"] = exp.
A name must be:
Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.
This means you have to use the second form of the table constructor ["name"] = exp for anything that isn't valid identifier.
['chief-of-police'] = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_PUMPSHOTGUN', components = { 2000, 6000, nil }, price = 70000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
}
Error in your code "private" reserved word:
>>>>private = {
{ weapon = 'WEAPON_APPISTOL', components = { 0, 0, 1000, 4000, nil }, price = 10000 },
{ weapon = 'WEAPON_ADVANCEDRIFLE', components = { 0, 6000, 1000, 4000, 8000, nil }, price = 50000 },
{ weapon = 'WEAPON_NIGHTSTICK', price = 0 },
{ weapon = 'WEAPON_STUNGUN', price = 500 },
{ weapon = 'WEAPON_FLASHLIGHT', price = 0 },
},
table must be constructed
myTable = {}
after table exists, you can define key:
myTable["some.key"] = {}
if you nead sub-key, next step will be:
myTable["some.key"]["sub.key"] = {}
myTable["some.key"]["sub.key"]["value_1"] = "123"
myTable["some.key"]["sub.key"]["value_2"] = "456"
at the end, you can use trick:
local myTables = {}
myTables["table_name"] = myTable
I am fetching the data from sqlite3, now i need to display that values in UIPicker view , Can any one help me please,
I got the response from data base like
stateName: (
{
"state_name" = Alabama;
},
{
"state_name" = Arizona;
},
{
"state_name" = Arkansas;
},
{
"state_name" = California;
},
{
"state_name" = Colorado;
},
{
"state_name" = Connecticut;
},
{
"state_name" = Delaware;
},
{
"state_name" = "District of Columbia";
},
{
"state_name" = Florida;
},
{
"state_name" = Georgia;
},
{
"state_name" = Idaho;
},
{
"state_name" = Illinois;
},
{
"state_name" = Indiana;
},
{
"state_name" = Iowa;
},
{
"state_name" = Kansas;
},
{
"state_name" = Kentucky;
},
{
"state_name" = Louisiana;
},
{
"state_name" = Maine;
},
{
"state_name" = Maryland;
},
{
"state_name" = Massachusetts;
},
{
"state_name" = Michigan;
},
{
"state_name" = Minnesota;
},
{
"state_name" = Mississippi;
},
{
"state_name" = Missouri;
},
{
"state_name" = Montana;
},
{
"state_name" = Nebraska;
},
{
"state_name" = Nevada;
},
{
"state_name" = "New Hampshire";
},
{
"state_name" = "New Jersey";
},
{
"state_name" = "New Mexico";
},
{
"state_name" = "New York";
},
{
"state_name" = "North Carolina";
},
{
"state_name" = "North Dakota";
},
{
"state_name" = Ohio;
},
{
"state_name" = Oklahoma;
},
{
"state_name" = Oregon;
},
{
"state_name" = Pennsylvania;
},
{
"state_name" = "Rhode Island";
},
{
"state_name" = "South Carolina";
},
{
"state_name" = "South Dakota";
},
{
"state_name" = Tennessee;
},
{
"state_name" = Texas;
},
{
"state_name" = Utah;
},
{
"state_name" = Vermont;
},
{
"state_name" = Virginia;
},
{
"state_name" = Washington;
},
{
"state_name" = "West Virginia";
},
{
"state_name" = Wisconsin;
},
{
"state_name" = Wyoming;
}
)
There is the delegate method of UIPickerView. #pragma mark - PickerView Delegates
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//=====================================================================================
//Customize Number of rows in Component
//=====================================================================================
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if([genreList count] != 0)
{
return [genreList count];
}
else
{
return 0;
}
}
//=====================================================================================
//Set title For Row
//=====================================================================================
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row1 forComponent:(NSInteger)component {
if([genreList count] != 0)
{
return [genreList objectAtIndex:row1];
}
else
{
return nil;
}
//return 4;
}