How to get the final counter values in dictionary on iOS - ios

I have array of dictionary values and have implemented the counter method for individual array items but had problem with getting the final counter values. it was adding individual counter values.
here is the source code.
-(void)getData
{
[lReportArr removeAllObjects];
lMetaCount = 0;
NSMutableArray *lTempArr = [[NSMutableArray alloc] initWithArray:lMetaArray];
NSLog(#"Metabolic Array:-%#", lTempArr);
for (int i = 0; i < [lTempArr count]; i++)
{
NSMutableDictionary *lDict = [lTempArr objectAtIndex:i];
NSString *lTitle = [lDict objectForKey:#"title"];
//Glucose Low count
if ([lTitle rangeOfString:#"Glucose reading : Low"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"Low BG:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
//Glucose High count
else if ([lTitle rangeOfString:#"Glucose reading : High"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"High BG:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
//Wrong Dose Cause count
else if([lTitle rangeOfString:#"Low Cause: Wrong Dose"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"LG Wrong Dose:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
else if([lTitle rangeOfString:#"High Cause: Wrong Dose"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"HG Wrong Dose:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
//Symptoms Cause count
else if([lTitle rangeOfString:#"Low Cause: Illness"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"LG Illness:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
else if([lTitle rangeOfString:#"High Cause: Illness"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"HG Symptoms:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
//LG n HG Treatment count
else if([lTitle rangeOfString:#"Treatment: Emergency Room"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"Emergency Room:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
else if([lTitle rangeOfString:#"Treatment: Hospitalization"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"Hospitalization:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
else if([lTitle rangeOfString:#"Treatment: Extra MD Office"].length > 0)
{
lMetaCount++;
NSString *lCount = [NSString stringWithFormat:#"Extra MD Office:%d",lMetaCount];
[lDict setValue:lCount forKey:#"metabolicCount"];
}
[lReportArr addObject:lDict];
}
}

you can use
int c = [[lReportArr objectAtIndex:0] valueForKey:#"metabolicCount"];
this will give you the final count of metabolicCount value for first object stored in dictionary if it contains metabolicCount key

Related

NSMutableArray have restriction in adding object?

I am using NSMutableArray for adding objects.But its add only first 10 objects.
I have code for sharing
for (int j = 0; j<[feedData count]; j++)
{
[sharingItems addObject:[self whatsappdata:[feedData objectAtIndex:j]]];
}
This method return NSString type text.
Please provide me valid solution for this.
Thanks in Advance
-(NSString *)whatsappdata:(NSDictionary *)cellData1
{
NSString *brandName = [NSString stringWithFormat:#"%#", [cellData1 objectForKey:#"brand_name"]];
NSString *modelName = [NSString stringWithFormat:#"%#", [cellData1 objectForKey:#"brand_model_name"]];
NSString *version = [NSString stringWithFormat:#"%#", [cellData1 objectForKey:#"version_name"]];
if ([version isEqualToString: #"<null>"])
{
version = #"";
}
NSString *year = [NSString stringWithFormat:#"%#", [cellData1 objectForKey:#"model_year"]];
if (year == nil || [year isEqualToString:#"0"])
{
year = #"";
}
NSString *inventoryValue = [NSString stringWithFormat:#"%#",[cellData1 objectForKey:#"inventory_type"]];
NSInteger value = [inventoryValue intValue];
NSString *inventoryName;
NSString *msg;
if(value == 1)
{
inventoryName = [NSString stringWithFormat:#"%#", #"Stock"];
i++;
NSString *text2 = [NSString stringWithFormat:#"%d.%# %# %#- %# Single Owner\n",i, brandName, modelName, version, year];
msg = [NSString stringWithFormat:#"%#",text2];
msg= [msg stringByReplacingOccurrencesOfString:#"\n" withString:#"<br/>"];
}
else
{
inventoryName = [NSString stringWithFormat:#"%#", #"Required"];
msg = #"";
}
return msg;
//end data
}
Most probably "fetch limit" has set for 'NSFetchRequest' inside your code.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setFetchLimit:10];//such code you need to find and remove/change fetch limit
you need to allocate memory to array before adding elements
sharingItems = [[NSMutableArray alloc]init];

Sort NSMutableArray by part of each string as a date

I have an NSMuableArray with each string containing lots of information(i know its not the best way but its how I'm doing it) so each string looks something like
02:00:00-21:00:00-June 3th, 2014-23:00:00-June 3th, 2014||VincentKahn.com||10.00
I want to sort it by the first date, the 21:00:00-June 3th, 2014 part (i know there are typos), I've seen examples of NSSortDescriptor but i don't just have a date, and i still need to retain all of the information.
I've tried splitting it but that was in a for each loop and if i did it how i know i will loose the formatting which i need.
What's the quickest way to do Sort this?
If it helps i'm sorting it for a TableView
EDIT:
I'm retrieving these strings saved in a .plist opened into a mutable array
Complicated Approach, but i ended up using a triple sorted system using blocks (i think thats the name) Sorted the day, then the month then the year.
-(void)sortTable {
NSString *Ja = #"January";
NSString *Fe = #"February";
NSString *Ma = #"March";
NSString *Ap = #"April";
NSString *Mar = #"May";
NSString *Ju = #"June";
NSString *Jul = #"July";
NSString *Au = #"August";
NSString *Se = #"September";
NSString *Oc = #"October";
NSString *No = #"November";
NSString *De = #"December";
NSArray *sortedDay = [arrayFromPlist sortedArrayUsingComparator:^NSComparisonResult(id a, id b)
{
NSString *aString = (NSString*) a;
NSString *dateA = [[aString componentsSeparatedByString:#"-"] objectAtIndex:2];
NSString *thePreDayA = [[dateA componentsSeparatedByString:#" "] objectAtIndex:1];
NSString *almostDayA = [[thePreDayA componentsSeparatedByString:#","] objectAtIndex:0];
if ([almostDayA rangeOfString:#"th"].location != NSNotFound) {
NSLog (#"Yes, we have a th at location %i", [almostDayA rangeOfString:#"th"].location );
theDayA = [[almostDayA componentsSeparatedByString:#"th"] objectAtIndex:0];
} else {
theDayA = almostDayA;
}
int aInt = [theDayA integerValue];
NSString *bString = (NSString*) b;
NSString *dateB = [[bString componentsSeparatedByString:#"-"] objectAtIndex:2];
NSString *thePreDayB = [[dateB componentsSeparatedByString:#" "] objectAtIndex:1];
NSString *almostDayB = [[thePreDayB componentsSeparatedByString:#","] objectAtIndex:0];
if ([almostDayB rangeOfString:#"th"].location != NSNotFound) {
NSLog (#"Yes, we have a th at location %i", [almostDayB rangeOfString:#"th"].location );
theDayB = [[almostDayB componentsSeparatedByString:#"th"] objectAtIndex:0];
} else {
theDayB = almostDayB;
}
int bInt = [theDayB integerValue];
return aInt > bInt;
}];
NSArray *sortedMonths = [sortedDay sortedArrayUsingComparator:^NSComparisonResult (id a, id b)
{
NSString *aString = (NSString*) a;
NSString *dateA = [[aString componentsSeparatedByString:#"-"] objectAtIndex:2];
NSString * preMonthA = [[dateA componentsSeparatedByString:#" "] objectAtIndex:0];
int aInt;
if([preMonthA isEqualToString:Ja]) {
aInt = 1;
} else if([preMonthA isEqualToString:Fe]) {
aInt = 2;
} else if([preMonthA isEqualToString:Mar]) {
aInt = 3;
} else if([preMonthA isEqualToString:Ap]) {
aInt = 4;
} else if([preMonthA isEqualToString:Ma]) {
aInt = 5;
} else if([preMonthA isEqualToString:Ju]) {
aInt = 6;
} else if([preMonthA isEqualToString:Jul]) {
aInt = 7;
} else if([preMonthA isEqualToString:Au]) {
aInt = 8;
} else if([preMonthA isEqualToString:Se]) {
aInt = 9;
} else if([preMonthA isEqualToString:Oc]) {
aInt = 10;
} else if([preMonthA isEqualToString:No]) {
aInt = 11;
} else if([preMonthA isEqualToString:De]) {
aInt = 12;
} else {
aInt = 0;
}
NSString *bString = (NSString*) b;
NSString *dateB = [[bString componentsSeparatedByString:#"-"] objectAtIndex:2];
NSString *preMonthB = [[dateB componentsSeparatedByString:#" "] objectAtIndex:0];
int bInt;
if([ preMonthB isEqualToString:Ja]) {
bInt = 1;
} else if([ preMonthB isEqualToString:Fe]) {
bInt = 2;
} else if([ preMonthB isEqualToString:Mar]) {
bInt = 3;
} else if([ preMonthB isEqualToString:Ap]) {
bInt = 4;
} else if([ preMonthB isEqualToString:Ma]) {
bInt = 5;
} else if([ preMonthB isEqualToString:Ju]) {
bInt = 6;
} else if([ preMonthB isEqualToString:Jul]) {
bInt = 7;
} else if([ preMonthB isEqualToString:Au]) {
bInt = 8;
} else if([ preMonthB isEqualToString:Se]) {
bInt = 9;
} else if([ preMonthB isEqualToString:Oc]) {
bInt = 10;
} else if([ preMonthB isEqualToString:No]) {
bInt = 11;
} else if([ preMonthB isEqualToString:De]) {
bInt = 12;
} else {
bInt = 0;
}
return aInt > bInt;
}];
NSArray *sortedYear = [sortedMonths sortedArrayUsingComparator:^NSComparisonResult(id a, id b)
{
NSString *aString = (NSString*) a;
NSString *dateA = [[aString componentsSeparatedByString:#"-"] objectAtIndex:2];
int aInt = [[[dateA componentsSeparatedByString:#" "] objectAtIndex:2] integerValue];
NSString *bString = (NSString*) b;
NSString *dateB = [[bString componentsSeparatedByString:#"-"] objectAtIndex:2];
int bInt = [[[dateB componentsSeparatedByString:#" "] objectAtIndex:2] integerValue];
return aInt > bInt;
}];
NSLog(#"SORTED ARRRAYYYYYYYY: %#", sortedYear);
}

NSString has same value twice

I have a function to initialize an array of backpack objects, and a property of the backpack object is positive, negative, and neutral attributes, which are all NSStrings. The problem is that when I initialize the values of the attributes, the same value repeats twice (when I log the value of it)
The code:
#pragma mark - Creating Backpack Icons
-(NSArray *)createBackpackIcons:(NSArray *)groups usingItemGroups:(NSArray *)items
{
NSMutableArray *backpackItems = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < _groups.count; i++) {
Group *group = _groups[i];
NSString *defindex1 = [NSString stringWithFormat:#"%#", group.defindex];
for (NSInteger j = 0; j < _itemGroups.count; j++)
{
Item *item = _itemGroups[j];
NSString *defindex2 = [NSString stringWithFormat:#"%#", item.defindex];
if([defindex1 isEqualToString:defindex2])
{
//NSLog(#"%#", item.item_name);
backpackIcons *backpack = [[backpackIcons alloc] init];
backpack.pos_att = #"";
backpack.neg_att = #"";
backpack.neutral_att = #"";
//name of item
if([item.proper_name boolValue])
{
backpack.name = #"The ";
backpack.name = [backpack.name stringByAppendingString:item.item_name];
}
else
{
backpack.name = item.item_name;
}
backpack.original_id = group.original_id; //id of the item
backpack.defindex = item.defindex; //defindex number of the item
backpack.level = group.level; //level of the item
backpack.quality = group.quality; //quality of the item
backpack.image_url = item.image_url; //image of the item
backpack.item_description = item.item_description; //description of the item
backpack.image_url_large = item.image_url_large; //large image of the item
backpack.item_type = item.item_type_name; //type of item
//find origin of item
if(group.origin != NULL) {
backpack.origin = [item.originNames objectAtIndex:[group.origin integerValue]];
}
else {
backpack.origin = #"";
}
if([group.flag_cannot_craft boolValue])
{
//item isn't craftable
backpack.not_craftable = #"(Not Craftable)";
}
if([group.flag_cannot_trade boolValue])
{
//item isn't tradable
backpack.not_tradable = #"(Not Tradable)";
}
//custom name
if(group.custom_name != NULL) {
backpack.custom_name = group.custom_name;
}
//custom description
if(group.custom_desc != NULL) {
backpack.custom_desc = group.custom_desc;
}
//find positive and negative attributes of the item
if(group.attributes.count != 0) {
for(NSInteger num = 0; num < group.attributes.count; num++) {
NSString *att_def = [NSString stringWithFormat:#"%#",[[group.attributes objectAtIndex:num] valueForKey:#"defindex"]];
//NSLog(att_def);
for(NSInteger num2 = 0; num2 < item.attributes.count; num2++) {
NSString *att_def2 = [NSString stringWithFormat:#"%#", [[item.attributes objectAtIndex:num2] valueForKey:#"defindex"]];
//defindex of 214 is for counting kills on strange weapons
if(att_def2 != NULL) {
if ([att_def isEqualToString:att_def2]) {
//we have found the attribute!
if(![[[item.attributes objectAtIndex:num2] valueForKey:#"hidden"] boolValue]) {
//item isn't hidden from view
if(![[[item.attributes objectAtIndex:num2] valueForKey:#"stored_as_integer"] boolValue]) {
//we use the float value
if([[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] isEqualToString:#"1"] || [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] isEqualToString:#"0"]) {
//the attribute is string only, it has no values
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
NSString *attribute = [[item.attributes objectAtIndex:num2] valueForKey:#"description_string"];
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
else {
//the attribute uses values given my the float_value key
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
double y = [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] doubleValue];
NSInteger z;
if([[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] rangeOfString:#"%s1%"].location != NSNotFound) {
//for values with percentages
z = lroundf(y*100);
if(z >= 100) {
z = z - 100;
}
}
else {
//for values that are just integers
z = y;
}
NSString *a = [NSString stringWithFormat:#"%d", z];
NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] stringByReplacingOccurrencesOfString:#"%s1" withString:a];
//NSLog(attribute);
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
}
else {
//we use the integer value
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
NSInteger y = [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"value"]] integerValue];
NSString *a = [NSString stringWithFormat:#"%d", y];
NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] stringByReplacingOccurrencesOfString:#"%s1" withString:a];
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
}
}
}
}
}
}
[backpackItems addObject:backpack];
}
}
}
return backpackItems;
}
The actual code to set the values of the attributes:
if(![[[item.attributes objectAtIndex:num2] valueForKey:#"stored_as_integer"] boolValue]) {
//we use the float value
if([[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] isEqualToString:#"1"] || [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] isEqualToString:#"0"]) {
//the attribute is string only, it has no values
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
NSString *attribute = [[item.attributes objectAtIndex:num2] valueForKey:#"description_string"];
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
else {
//the attribute uses values given my the float_value key
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
double y = [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"float_value"]] doubleValue];
NSInteger z;
if([[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] rangeOfString:#"%s1%"].location != NSNotFound) {
//for values with percentages
z = lroundf(y*100);
if(z >= 100) {
z = z - 100;
}
}
else {
//for values that are just integers
z = y;
}
NSString *a = [NSString stringWithFormat:#"%d", z];
NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] stringByReplacingOccurrencesOfString:#"%s1" withString:a];
//NSLog(attribute);
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
}
else {
//we use the integer value
NSString *att_type = [[item.attributes objectAtIndex:num2] valueForKey:#"effect_type"];
NSInteger y = [[NSString stringWithFormat:#"%#", [[group.attributes objectAtIndex:num] valueForKey:#"value"]] integerValue];
NSString *a = [NSString stringWithFormat:#"%d", y];
NSString *attribute = [[[item.attributes objectAtIndex:num2] valueForKey:#"description_string"] stringByReplacingOccurrencesOfString:#"%s1" withString:a];
if([att_type isEqualToString:#"positive"]) {
backpack.pos_att = [backpack.pos_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else if ([att_type isEqualToString:#"negative"]) {
backpack.neg_att = [backpack.neg_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
} else {
backpack.neutral_att = [backpack.neutral_att stringByAppendingString:[NSString stringWithFormat:#"%#\n", attribute]];
}
}
}
It seems that the problem was that my array groups.attributes containing the attributes for each item had each attribute twice, so I initialized it wrong. Thanks to everyone who helped.

Objective-C hashtag/tags autocomplete implementation

I was looking into implementing hashtag autocomplete with objective-C as shown in the picture
I found it a bit difficult than expected. I'm looking more specific implementation for adding and deleting hashtags. For example, the hashtag should be deleted as a whole at once. I was wondering if anyone has similar experience implemented it and if there's a more efficient way implemented it. Thanks
I ended up writing some functions that I feel is a bit ugly but it works. Maybe there are some more efficient ways to implement it.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//user is a singleton instance
User *user = [User sharedUser];
user.autocompleteTableView.hidden = NO;
int identifiedTagsStringLength = [self.identifiedTagsString length];
int cursorLocation = range.location;
//insert characters
if (range.location >= identifiedTagsStringLength) {
NSString *newSearch =#"";
NSRange newRange;
if(identifiedTagsStringLength != 0) {
newSearch = [urlField.text substringFromIndex:identifiedTagsStringLength];
newRange = NSMakeRange(range.location - identifiedTagsStringLength, 0);
}
else {
newSearch = textField.text;
newRange = range;
}
NSString *substring = [NSString stringWithString:newSearch];
substring = [substring stringByReplacingCharactersInRange:newRange withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
if (cursorLocation > currentTagsRange) {
currentTagsRange = cursorLocation;
}
}
//delete tags
else {
if ([self.ranges count] != 0 && cursorLocation < currentTagsRange) {
int rangeLength = [self.ranges count];
int toBeRemovedIndex = 0;
for (int i = 0; i< rangeLength; i++) {
if (cursorLocation >= [[self.ranges objectAtIndex:i][0] intValue]
&& cursorLocation <= [[self.ranges objectAtIndex:i][1] intValue]) {
toBeRemovedIndex = i;
}
}
[self.tags removeObjectAtIndex:toBeRemovedIndex];
[self updateRanges];
NSString *outputString = #"";
for (NSString *tag in self.tags) {
outputString = [NSString stringWithFormat:#"%##%# ", outputString,
tag];
}
urlField.text = outputString;
self.identifiedTagsString = urlField.text;
currentTagsRange = [outputString length] - 1;
}
}
return YES;
}
- (void)updateRanges {
self.ranges = [[NSMutableArray alloc] init];
int startIndex = 0;
for (NSString *tag in self.tags) {
startIndex = [self.ranges count] == 0 ? 0 : [[self.ranges lastObject][1] intValue] + 1;
int tagLength = [tag length];
NSArray *range = [NSArray arrayWithObjects:[NSNumber numberWithInt:startIndex], [NSNumber numberWithInt:startIndex + tagLength + 1], nil];
[self.ranges addObject: range];
}
}
#pragma mark UITableViewDataSource methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (self.identifiedTagsString == NULL) {
self.identifiedTagsString = #"";
}
[self.tags addObject: selectedCell.textLabel.text];
[self updateRanges];
NSString *output = #"";
for (NSString *tag in self.tags) {
output = [NSString stringWithFormat:#"%##%# ", output, tag];
}
urlField.text = output;
User *user = [User sharedUser];
user.autocompleteTableView.hidden = YES;
self.identifiedTagsString = urlField.text;
currentTagsRange = [urlField.text length];
}

Printing from NSMutableArray

So i want to print the users in an NSMutableArray. But the strings keep coming out as nil.
here is what i have:
int users = 0;
- (IBAction)addNewUser:(id)sender {
NSString *string;
string = userNameTextField.text;
[usernameArray insertObject:string atIndex:users];
users++;
[self showUsers];
}
-(void)showUsers{
for (int i = 0; i < users; i++){
NSString *s = textView.text;
NSString *add;
add = [NSString stringWithFormat:#"%# ",[usernameArray objectAtIndex:i]];
NSString *display = [NSString stringWithFormat:#"%# \n %#", s, add];
textView.text = display;
}
}
i have also tried
-(void)showUsers{
for (int i = 1; i < users; i++){
NSString *s = textView.text;
NSString *add;
add = [usernameArray objectAtIndex:i];
NSString *display = [NSString stringWithFormat:#"%# \n %#", s, add];
textView.text = display;
}
}
First of all try using more comprehensive names for the objects. I'm rewriting your code.
Common Causes for the problem : Array not initialized, you are starting your for cycle with int i equal to 1, so you are missing the object at index 0 at your mutable array. Try the following code.
#interface InterfaceName : InterfaceInherits <IfDelegate> {
int usersCount;
NSMutableArray * usernameArray;
}
#implementation InterfaceName
/*There's no more confident way to initialize a variable than in the init method of the class. */
-(id)init{
usersCount = 0;
//You have to be sure that your array is not nil
usernameArray = [NSMutableArray alloc]init]];
return self;
}
- (IBAction)addNewUser:(id)sender {
NSString *username = [usernameTextField text];
[usernameArray insertObject:username atIndex:usersCount];
usersCount++;
//I'll omit the display as I'm not sure what you were doing with it.
}
-(void)showUsers{
for (int i = 0; i < usersCount; i++){
NSString *retrievedUser = [usernameArray objectAtIndex:i];
NSString *display = [NSString stringWithFormat:#"User Retrieved : %#",retrievedUser];
textView.text = display;
}
}
#end

Resources