I have a method called addbuttonClicktocounter. When the function is called it should add the button name as key and number of clicks as count. I made this in my static library. When the user called these method again and again it should capture all the button names and number of clicks in one dictionary. If same button clicks again and again means the button name should remain same and the click count oly get increased. Here is my code what my tried upto my level:
NSMutableDictionary *BtnclicDict;
-(void) addButtonClickToCounter : (NSString*)button_Name button_click :(int)but_Click{
if([[BtnclicDict allKeys] containsObject:button_Name]){
int saveClick = [[BtnclicDict valueForKey:button_Name] integerValue];
but_Click = saveClick + but_Click;
NSNumber *click = [NSNumber numberWithInt:but_Click];
NSString *clickString = [click stringValue];
[BtnclicDict setObject:clickString forKey:button_Name];
NSLog(#"same button...,%#",click);
NSLog(#"same key dict...,%#",BtnclicDict);
} else {
NSString *but_Name = [NSString stringWithString:button_Name];
// NSLog(#"%#",but_Name);
NSNumber *click = [NSNumber numberWithInt:but_Click];
// NSLog(#"%#",click);
NSString *clickString = [click stringValue];
// BtnclicDict = [NSMutableDictionary dictionaryWithObject:but_Name forKey:click];
// BtnclicDict = [[NSMutableDictionary alloc]init];
[BtnclicDict setObject:clickString forKey:but_Name];
NSLog(#"working,%#",BtnclicDict);
// NSLog(#"%# Button Values...",BtnclicDict);
}
}
If you call this method from another class again and again it should collect all the details and make it into one dictionary.
You can try this.
-(void) addButtonClickToCounter : (NSString*)button_Name button_click :(int)but_Click{
if ([BtnclicDict valueForKey:buttonName])
but_Click += [[BtnclicDict valueForKey:button_Name] integerValue];
[BtnclicDict setValue:but_Click forKey:button_Name];
}
Related
I'm new to IOS and I'm not sure if I'm on the right track. What I need to know is if I'm on the right track and if I'm off it a hint on what to fix so I can get back on track. The mutable Array should read an array of speakers and say "Hello, my name is <speakerArray>" it should do that 8 times with a different name each time. This is what I Have:
- (NSArray*)badgesForSpeakers:(NSArray*)speakers {
for(speakers i = 0; i => 7; i++)
{
NSString *greetings =#"Hello, my name is .";
NSMutableArray *badges = [speakers arrayByAddingObjectsFromArray:greetings];
}
return badges;
}
Let's take this one step at a time. First of all, your operator in the loop is wrong; you mean to execute while i is less than or equal to 7. Thus, change => to <=. However, it's more stylish to say i < 8. And finally, it's most stylish of all to use what's called "Fast Enumeration", which allows you to loop without an index at all. In fact, it will work no matter how many items are in your speakers array! That takes us here:
- (NSArray*)badgesForSpeakers:(NSArray*)speakers {
for (NSString* speaker in speakers)
{
NSString *greetings =#"Hello, my name is .";
NSMutableArray *badges = [speakers arrayByAddingObjectsFromArray:greetings];
}
return badges;
}
Next, greetings isn't an array! It's a string. That's why calling -arrayByAddingObjectsFromArray: doesn't make any sense, and why the compiler isn't going to like it. Let's make its name singular, greeting, to reflect this fact. Strategy: Your goal here is to create an empty array, then construct items one by one and add them to that array. That takes us to:
- (NSArray*)badgesForSpeakers:(NSArray*)speakers {
NSMutableArray *badges = [NSMutableArray array]; //Here we make an empty array
for (NSString* speaker in speakers)
{
NSString *greeting =#"Hello, my name is .";
[badges addObject:greeting]; //Here we add one item to it each time 'round the loop
}
return badges;
}
Last, your string has no interpolation right now! It reads literally "Hello, my name is ." We do string interpolation using the -stringWithFormat: method.
Finished Product:
- (NSArray*)badgesForSpeakers:(NSArray*)speakers {
NSMutableArray *badges = [NSMutableArray array];
for (NSString* speaker in speakers)
{
NSString *greeting = [NSString stringWithFormat:#"Hello, my name is %#.",speaker];
[badges addObject:greeting];
}
return badges;
}
That should get you started with fast enumeration and string interpolation. Remember to compile your code often and try to understand the compiler errors--it would have helped you with some of these issues.
Maybe you mean this
- (NSMutableArray *)badgesForSpeakers:(NSArray *)speakers {
NSMutableArray *badges = [[NSMutableArray alloc] init];
for (NSString *speaker in speakers) {
[badges addObject:[NSString stringWithFormat:#"Hello, my name is %#", speaker]];
}
return badges;
}
plz use this code
- (NSArray*)badgesForSpeakers:(NSArray*)speakers {
NSMutableArray *badges = [NSMutableArray alloc];
for(int i = 0; i < speakers.count; i++)
{
NSString *greetings =[NSString stringWithFormat:#"Hello, my name is .%#",[speakers objectAtIndex:i]];
badges = [speakers addObject:greetings];
}
return [badges copy];
}
In my project I am getting response from the server in the form
response:
<JKArray 0x7fa2e09036b0>(
{
id = 23;
name = "Name1";
},
{
id = 24;
name = "Name2";
}
)
From this response array i am retrieving the objects at different indexes and then adding them in a mutableArray and then into a contactsDictionary.
self.contactsDictionary = [[NSMutableDictionary alloc] init];
for(int i=0 ; i < [response count] ; i++)
{
NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
[mutableArray addObject:[response objectAtIndex:i]];
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
}
I want to retrieve data for Key #"name" from the contactsDictionary at some other location in the project. So how to do it.
Thanks in advance....
this is the wrong way like you are setting your contactsDictionary.
replace below line
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
with
[self.contactsDictionary setObject:[mutableArray objectAtIndex :i] forKey:[NSString stringWithFormat:#"%i",i]];
becuase everytime your array have new objects so your contacts dictionary's first value have one object then second value have two object. so you shouldn't do that.
now, if you want to retrieve name then call like
NSString *name = [[self.contactsDictionary objectForKey : #"1"]valueForKey : #"name"];
avoid syntax mistake if any because have typed ans here.
Update as per comment:
just take one mutablearray for exa,
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject : name]; //add name string like this
hope this will help :)
Aloha from your respond I can give you answer Belo like that according to you response.
for(int i=0;i<[arrRes count];i++);
{
NSString *strId = [NSString stringWithFormat:#"%#",[[arrRes obectAtIndex:i]objectForKey:#"id"]];
NSString *StrName = [NSString stringWithFormat:#"%#",[[arrRes objectAtIndex:i]objectForKey:#"name"]];
NSLog(#"The ID is -%#",strId);
NSLog(#"The NAME is - %#",strName);
}
I have a global variable in FirstViewController.h
extern NSString *dateString;
it records the date/time of when I press saveText button. There is another button called readText which pushes a UITableView. The first cell on this table will print the date saved.
However, this only works if I first press the saveText button. Otherwise, if I just press readText, it crashes. How can I get it so that if there is no current datetime saved, that it doesn't crash?
Here is where I wrote the timestamp for saveButton:
-(IBAction)saveText:(id)sender{
//code to save text, irrelevant to the question//
dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle];
NSLog(#"%#",dateString);
}
and here is the code to load the tableviewcells with the timestamp
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = #"Text- saved on: ";
str = [str stringByAppendingString: dateString];
self.list = [[NSArray alloc] initWithObjects: str, nil];
}
In my methods that use the variable dateString,
I used:
if([dateString length] == 0){
//perform standard procedure
}
else {
//what I want it to do
}
Hi I have NSarray values in Xcode. I need to get array indexOfObject by comparing string values.
my array values are
(
{
firstName = lord;
lastname = krishna;
},
{
firstName = priya;
lastname = amirtha;
}
)
If I type first name in textfield and click button means last name want to display in another textfield.
thank you.
To answer the title of your question:
NSString *compareString = #"something";
NSMutableArray *indexesOfMatches = [[NSMutableArray alloc]init];
for (NSString *string in theArray) {
if ([string isEqualToString:compareString]) {
NSNumber *index = [[NSNumber numberWithInterger:[theArray indexOfObject:string]];
[indexOfMatches addObject:index];
}
}
//indexOfMatches will now contain NSNumber objects that represent the indexes of each of the matching string objects in the array
I think that using an NSDictionary would be better for you though. Then you can simply keep the first and last names as Key Value pairs.
NSDictionary *names = #{#"lord" : #"krishna", #"priya" : #"amirtha" };
Then you can just do value for key when you get the first name:
NSString *firstName = #"lord";
NSString *lastName = [names valueForKey:firstName];
Store firstNameArray and lastNameArray a mutable array NSMutableArray.
Using Fast Enumeration. Suppose array is the array you are provided with
for (NSDictionary *item in array) {
[firstNameArray addObject:[item objectForKey:#"firstName"]];
[lastNameArray addObject:[item objectForKey:#"lastName"]];
}
After entering the data in firstNameTextField click the button
Button action method implementation
-(IBAction)btnClicked:(id)sender {
NSInteger index = [firstName indexOfObject:[firstNameTextField text]];
[lastNameTextField setText:[lastName objectAtIndex:index]];
}
I'm now playing song from iPod Library that loaded into myArray with iPodMusicPlayer.
We can use indexOfNowPlayingItem to get index from NowPlaying music.
But when i Shuffle Mode is on, that indexOfNowPlayingItem Property's return index is completely wrong.
before ShuffleMode is off,indexOfNowPlayingItem can be used and correct.
However when ShuffleMode is on, indexOfNowPlayingItem count is only increase 1 (++).
like that
indexOfNowPlayingItem++;
Not a correct for ShuffleMode on.
So how can i get correct index when ShuffleMode on?
Thanks for your help.
My solution was when you set your playlist, set also a NSMutableDictionary with (index, MPMediaEntityPropertyPersistentID) of playlist.
When you need the song index, just get with the song persistent id.
- (void) setPlaylistIndexDictionary:(MPMediaItemCollection *)playlistItems
{
playlistIndexDict = [[NSMutableDictionary alloc] init];
NSNumber *count = [[NSNumber alloc] initWithInt:0];
for (MPMediaItem *item in [playlistItems items]) {
[playlistIndexDict setObject:count forKey:[item valueForProperty:MPMediaEntityPropertyPersistentID]];
count = [NSNumber numberWithInt:count.intValue + 1];
}
}
- (NSString *) getCurrentSongId
{
NSString* songId = [[musicPlayer nowPlayingItem] valueForProperty:MPMediaEntityPropertyPersistentID];
return songId;
}
Using:
NSString *songId = [musicController getCurrentSongId];
int songIndex = [[[musicController playlistIndexDict] objectForKey:songId] intValue];