I have an original code as below which is working properly:-
- (ZYSideSlipFilterRegionModel *)commonFilterRegionModelWithKeyword:(NSString *)keyword selectionType:(CommonTableViewCellSelectionType)selectionType {
ZYSideSlipFilterRegionModel *model = [[ZYSideSlipFilterRegionModel alloc] init];
model.containerCellClass = #"SideSlipCommonTableViewCell";
model.regionTitle = keyword;
model.customDict = #{REGION_SELECTION_TYPE:#(selectionType)};
model.itemList = #[[self createItemModelWithTitle:[NSString stringWithFormat:#"Local"] itemId:#"0" selected:NO],
[self createItemModelWithTitle:[NSString stringWithFormat:#"Oversea"] itemId:#"1" selected:NO]];
return model;
}
Now, I plan to change the static value (Oversea / local) to dynamic value. But only 1 item will be displayed.
for (int i = 0; i < filteredArray.count; i++) {
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:[filteredArray[i] valueForKey:#"attribute_name"] itemId:myNewString selected:NO] ];
}
How can I put 2 item in model.itemList? Please help. Thank you.
You can use this way
//step:1 fetch Dictionary like this
for (int i = 0; i < filteredArray.count; i++)
{
NSMutableDictionary *dict = (NSMutableDictionary *)filteredArray[i] ;
int intItemID = i + 1;
NSString *myNewString = [NSString stringWithFormat:#"%i", intItemID];
model.itemList = #[[self createItemModelWithTitle:dict];
}
//Step 2 : You can define your method for Model Like this
- (CommonItemModel *)createItemModelWithTitle:(NSMutableDictionary *)dictModel
{
CommonItemModel *model = [[CommonItemModel alloc] init];
model.itemId = [dictModel valueForKey : #"itemId"];
model.itemName = [dictModel valueForKey:#"itemTitle"];
model.selected = [dictModel valueForKey:[NSNumber numberWithBool:
[[dictModel valueForKey:#"selected"]]]];
return model;
}
//One more thing you are write this in first step
model.itemList = #[[self createItemModelWithTitle:dict];
But the method only return the Model class (CommonItemModel) so if you need any help you shared here
Thanks :)
Hi i have two nsarrays.
Array A:
arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
Array B:
arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
I need to compare two arrays and get the value from array A by comparing with array B having value as selected. That is i should get german,chinese and russian sepearted by comma as nsstring.
Try this:
NSMutableArray *arrSelected = [[NSMutableArray alloc] init];
NSArray *arrProductSelection = [[NSArray alloc]initWithObjects:#"English",#"German",#"Russian",#"Chinese",#"Spanish",#"French",#"French",#"French",#"French",#"French",#"French",#"French",#"French",nil];
NSArray *arrProductSelectionB = [[NSArray alloc]initWithObjects:#"deselcted",#"selected",#"selected",#"selected",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",#"deselcted",nil];
for(int i = 0; i< arrProductSelectionB.count-1;i ++) {
if ([arrProductSelectionB[i] isEqualToString:#"selected"]) {
[arrSelected addObject:arrProductSelection[i]];
}
}
NSString *strSelected = [arrSelected componentsJoinedByString:#","];
NSLog(#"%#", strSelected);//output: German,Russian,Chinese
If you can make this type of array then manage easly.
(
{
flag = deselcted;
name = English;
},
{
flag = selected;
name = German;
},
{
flag = deselcted;
name = Russian;
},
{
flag = deselcted;
name = Chinese;
}
)
==> Array[(dictionary),(dictionary),.....]
if you need result in an array, Here is the function:
NSMutableArray*resultArray=[[NSMutableArray alloc]init];
for(int i=0; i<arrProductSelectionB.count;i++){
if ([[arrProductSelectionB objectAtIndex:i]isEqualToString:#"selected"]) {
[resultArray addObject:[arrProductSelection objectAtIndex:i]];
}
}
resultArrayhave the values which you need.
first i would make sure both arrays have same counters for safety something like
if (arrProductSelection.count == arrProductSelectionB) {
//than all you need is one for cycle something like :
for (int i = 0; i < arrProductionSelectionB.count; i++) {
if ([arrProductionSelectionB[i] isEqualToString: #"selected"]) {
do something magically with arrProductSelection[i];
}
}
}
I am trying to copy a specific values at specific index from one array to another like this:
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
arrayState is NSMutableArray and arrayTempState is NSArray
but arrayState is null every time.
I tried arrayState[i] = arrayTempState[i]; but it did not work.
arrayState was not initialised that is why it couldn't store the value.
plz try this
arrayState = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
I have 4 Event objects that would be displayed on a school timetable. I need to retrieve an array of the Events that clash with each other i.e. the start time of one Event is in between the start and end time of another. For the sake of getting the algorithm right first i have used int for the times instead of NSDate.
Event *event = [[Event alloc] init];
event.courseName = #"Maths";
event.room = #"405";
event.startTime = [NSNumber numberWithInt:8];
event.endTime = [NSNumber numberWithInt:10];
[eventsStore addObject:event];
Event *event2 = [[Event alloc] init];
event2.courseName = #"English";
event2.room = #"510";
event2.startTime = [NSNumber numberWithInt:10];
event2.endTime = [NSNumber numberWithInt:12];
[eventsStore addObject:event2];
Event *event3 = [[Event alloc] init];
event3.courseName = #"Computing";
event3.room = #"220";
event3.startTime = [NSNumber numberWithInt:11];
event3.endTime = [NSNumber numberWithInt:14];
[eventsStore addObject:event3];
Event *event4 = [[Event alloc] init];
event4.courseName = #"Sports";
event4.room = #"000";
event4.startTime = [NSNumber numberWithInt:13];
event4.endTime = [NSNumber numberWithInt:15];
[eventsStore addObject:event4];
Here is what i have at the moment to find the clashes:
int clashCounter = 0;
Event *curEv = nil, *otherEv = nil;
for(int i = 0; i < [eventsStore count]; i++)
{
curEv = [eventsStore objectAtIndex:i];
for (int j = 0; j < [eventsStore count]; j++)
{
if (j!=i && ![curEv.clashList containsObject:otherEv])
{
otherEv = [eventsStore objectAtIndex:j];
if (curEv.startTime < otherEv.endTime && otherEv.startTime < curEv.endTime)
{
clashCounter++;
NSLog(#"Clash: %# clashes with %#", curEv.courseName, otherEv.courseName);
[curEv.clashList addObject:otherEv];
}
}
}
}
Starts by creating two empty Event objects. Will loop through each Event in the eventsStore array, sets curEv to the objectAtIndex. Then loops through each of the eventsStore array objects that are not the curEv and does not have otherEv in its array. (each Event object contains an array called clashList for the Events it clashes with). If theres a clash then add otherEv to curEv.
When i run it i get:
ClashTest[3393:134117] Clash: English clashes with Computing
ClashTest[3393:134117] Clash: Computing clashes with English
ClashTest[3393:134117] Clash: Sports clashes with Computing
This is showing that the Event clashes are picked up but its storing a duplicate in the first instance(the English clashing with Computing goes both ways). But works as it is supposed to when it comes to the Sports and Computing clash because it only shows this once.
How can i get this working properly?
Apologies if this is deemed to specific, i've been messing around with this for a couple of weeks and haven't had much like no matter what ever way i try.
You could change the second loop to start with j = i+1 (and the first loop to stop at i < [eventsStore count] - 1). You would then go only over elements that are after the one checked.
I believe you could use a hash map to store the clashes, define this before your first for loop
NSMutableDictionary* clashes = [[NSMutableDictionary alloc] init];
Now when you find the clash (where you are logging it), check if the clash already exists both ways and if not, add it to the clashes dictionary like this (but you should have some unique id for the event, I'll assume it's an int e.g.)
NSString* firstHashKey = [NSString stringWithFormat:#"%d", curEv.Id];
NSString* secondHashKey = [NSString stringWithFormat:#"%d", otherEv.Id];
if (([clashes valueForKey:firstHashKey] == nil) && ([clashes valueForKey:secondHashKey] == nil)) {
clashes[firstHashKey] = #[ curEv, otherEv ];
}
else {
// It is already added to the clashes dictionary so no need to do it
}
Now when you want to print all the clashes you can simply do the following
for (NSString* key in clashes.allKeys) {
NSArray* value = clashes[key];
Event* firstEvent = value[0];
Event* secondEvent = value[1];
NSLog(#"Clash: %# clashes with %#", firstEvent.name, secondEvent.name);
}
The problem in the code is the initialization of otherEvent inside the inner loop...
for (int j = 0; j < [eventsStore count]; j++)
{
if (j!=i && ![curEv.clashList containsObject:otherEv]) // <-- look
{
// this happens too late to properly check the condition above
otherEv = [eventsStore objectAtIndex:j];
Change it like this...
for (int j = 0; j < [eventsStore count]; j++)
{
otherEv = [eventsStore objectAtIndex:j];
if (j!=i && ![curEv.clashList containsObject:otherEv])
{
and your logic will work. (Though there are quite a few opportunities for improvement, including shortening this inner loop as suggested above).
I'm having trouble with this block of code:
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
That last line throws an error:
"Redefinition of 'tempContact' with a different type
initWithJSONData: accepts NSDictionary
andDelay: int
I've tried to rewrite this code, with different types and all,
I'm just not sure what I'm doing
You already declared a variable in this scope named tempContact (NSArray *tempContact...). Change the name of one of them.
NSArray *tempContact and FlokContact *tempContact have the same name, that's the problem.
Change FlokContact *tempContact to FlokContact *temp_Contact or what you want.
try this
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
{
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
}