Problem with isEqualToString: method and NSInteger - iphone

This is my code:
for (int i=0; i<countingArray.count; i++) {
NSDictionary *element=[countingArray objectAtIndex:i];
NSString *source=[element objectForKey:#"id"];
NSInteger count= [[element objectForKey:#"count"] integerValue];
NSLog("source: %#",source); //Works
NSLog("count %d",count); //Don't Work! Error at this line
for(int c=0; c<subscriptions.count; c++) {
SubscriptionArray * element =[subscriptions objectAtIndex:c];
NSLog(#"sorgente sub %#",element.source);
NSLog(#"sorgente counting %#",source);
if([source isEqualToString:element.source]) {
element.count=count;
[subscriptions replaceObjectAtIndex:c withObject:element];
NSLog(#"equal");
//this part of code is never been executed but I'm sure that
//the if condition in some cases returns true
}
}
}
When I try to NSLog("count %d",count); my app crash without any information about.
I've also another problem with if([source isEqualToString:element.source]) I'm sure that some times the condition return true... How can I remove blank space? Like the trim function in php? thanks

Change:
NSString *source=[element objectForKey:#"id"];
NSInteger count= [[element objectForKey:#"count"] integerValue];
to:
NSString *source=[element valueForKey:#"id"];
NSInteger count= [[element valueForKey:#"count"] integerValue];
For removing blank spaces you can try:
NSString *trimmedString = [yourString stringByReplacingOccurrencesOfString:#" " withString:#""];

I didn't notice the first two NSLog statements. They're both of the form NSLog("something: %#", something). That is a C string literal, whereas NSLog takes an NSString for its format. This will lead to a crash. You want NSLog(#"source: %#", source).

Related

How to find words and full stop position from NSString in iOS?

I want to find any word or full stop from a nsstring.
NSString *str = #"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
In this I want to track the position of full stop not first full stop positions every full stop position in "Str".
I know how to find words but not able to get full stop position every time it come on my str.
Here is what am I doing
NSString *str = #"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:#" "];
for(int i=0;i<[arr count];i++)
{
if([[arr objectAtIndex:i] isEqualToString:#"."])
count++;
}
I don't want this as it give me str character length not by word. I want to check after how many words full stop is comming.
if ([str3 rangeOfString:#"."].location == xOut) { // dont want
or
if ([str3 rangeOfString:#"."].location != NSNotFound) { // dont want
Any Idea or suggestion would be highly welcome.
NSString *str = #"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
[str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationBySentences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[substring enumerateSubstringsInRange:NSMakeRange(0, substring.length) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSLog(#"%#", substring); // last word
*stop = YES;
}];
}];
This will give you the last word of each sentence.
Use this code -
NSString *str = #"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:#"."];
for(int i = 0; i< [arr count]; i++)
{
NSString *sentence = [arr objectAtIndex:i];
sentence = [sentence stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *wordArray = [sentence componentsSeparatedByString:#" "];
count = [wordArray count];
NSLog(#"No of words after full stop is comming = %i", count);
}
Try this
NSString *str = #"Hi, my name is Tina. I want to ask something. I am trying a lot. But I am not able. To find full stop. Position Everytime. It come in str.";
NSInteger count = 0;
NSArray *arr = [str componentsSeparatedByString:#" "];
for(int i=0;i<[arr count];i++)
{
NSString *arrStr=[arr objectAtIndex:i];
for(int j=0;j<arrStr.length;j++){
NSString *Schar=[arrStr substringWithRange:NSMakeRange(j, 1)];
if([Schar isEqualToString:#"."])
count++;
}
}
here,Count will show number of time . is in string.
Have a look # following links :
NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:#"."] count] - 1;
Number of occurrences of a substring in an NSString?
Number of Occurrences of a Character in NSString
Hope this helps :)

How to create a comma-separated string?

I want to create a comma-separated string like this.
NSString *list = #"iPhone,iPad,iPod";
I tried like this,
[strItemList appendString:[NSString stringWithFormat:#"%#,", [[arrItems objectAtIndex:i]objectForKey:#"ItemList"]]];
But the issue is I'm getting a string like this
#"iPhone,iPad,iPod," Note that there is an extra comma "," at the end of the string. How can I avoid that extra comma?
Can you please give me a hint. Highly appreciated
Thanks in advance
To join an array of strings into a single string by a separator (character which would be a string), you could use this method of NSArray class:
NSArray* array = #[#"iPhone", #"iPad", #"iPod"];
NSString* query = [array componentsJoinedByString:#","];
By using this method, you won't need to drop the last extra comma (or whatever) because it won't add it to the final string.
There's a couple of routes you can take.
If the number of items is always the same, and known before hand (which I guess isn't the case, but I mention it for completeness's sake), just make the whole string at once:
[NSString stringWithFormat:#"%#,%#,%#", [[arrItems objectAtIndex:0] objectForKey:#"ItemList"]], [[arrItems objectAtIndex:1] objectForKey:#"ItemList"]], [[arrItems objectAtIndex:2] objectForKey:#"ItemList"]]
Knowing that the unwanted comma will always be the last character in the string, you can make removing it the last step in construction:
} // End of loop
[strItemList removeCharactersInRange:(NSRange){[strItemList length] - 1, 1}];
Or you can change your thinking a little and do the loop like this:
NSString * comma = #"";
for( i = 0; i < [arrItems count]; i++ ){
[strItemList appendString:[NSString stringWithFormat:#"%#%#", comma, [[arrItems objectAtIndex:i]objectForKey:#"ItemList"]]];
comma = #",";
}
Notice that comma comes before the other item. Setting that string inside the loop means that nothing will be added on the first item, but a comma character will be for every other item.
After Completion of loop add below stmt
strItemList = [strItemList substringToIndex:[strItemList length]-1]
check the value of array count if array count is last then add without comma else add with comma. try this out i am not sure to much about.
if([arrItems objectAtIndex:i] == arrItems.count){
[strItemList appendString:[NSString stringWithFormat:#"%#", [[arrItems objectAtIndex:i]objectForKey:#"ItemList"]]];
}
else {
[strItemList appendString:[NSString stringWithFormat:#"%#,", [[arrItems objectAtIndex:i]objectForKey:#"ItemList"]]];
}
Assuming that arrItems is an NSArray with elements #"iPhone", #"iPad", and #"iPod", you can do this:
NSArray *list = [arrItems componentsJoinedByString:#","]
NSArray with elements #"iPhone", #"iPad", and #"iPod"
NSString *str=[[arrItems objectAtIndex:0]objectForKey:#"ItemList"]]
str = [str stringByAppendingFormat:#",%#",[[arrItems objectAtIndex:1]objectForKey:#"ItemList"]]];
str = [str stringByAppendingFormat:#",%#",[[arrItems objectAtIndex:2]objectForKey:#"ItemList"]]];
NsLog(#"%#",str);
// Assuming...
NSDictionary *dictionary1 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:#"iPhone", #"iPodTouch", nil] forKey:#"ItemList"];
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:#"iPad", #"iPad2", #"Apple TV", nil] forKey:#"ItemList"];
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:#"iMac", #"MacBook Pro", #"Mac Pro", nil] forKey:#"ItemList"];
NSArray *arrItems = [NSArray arrayWithObjects:dictionary1, dictionary2, dictionary3, nil];
// create string list
NSString *strItemList = [[arrItems valueForKeyPath:#"#unionOfArrays.ItemList"] componentsJoinedByString:#", "];
NSLog(#"All Items List: %#", strItemList);
Output:
All Items List: iPhone, iPodTouch, iPad, iPad2, Apple TV, iMac, MacBook Pro, Mac Pro
This method will return you the nsmutablestring with comma separated values from an array
-(NSMutableString *)strMutableFromArray:(NSMutableArray *)arr withSeperater:(NSString *)saperator
{
NSMutableString *strResult = [NSMutableString string];
for (int j=0; j<[arr count]; j++)
{
NSString *strBar = [arr objectAtIndex:j];
[strResult appendString:[NSString stringWithFormat:#"%#",strBar]];
if (j != [arr count]-1)
{
[strResult appendString:[NSString stringWithFormat:#"%#",seperator]];
}
}
return strResult;
}

convert a char to string

my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];

How to count '\n' in an UITextView

I got a headache trying to count returns (\n) in my UITextView. As you'll soon realise, I'm a bloody beginner and here is my theory of what I've come up with, but there are many gaps...
- (IBAction)countReturns:(id)sender {
int returns;
while ((textView = getchar()) != endOfString [if there is such a thing?])
{
if (textView = getchar()) == '\n') {
returns++;
}
}
NSString *newText = [[NSString alloc] initWithFormat:#"Number of returns: %d", returns];
numberReturns.text = newText;
[newText release];
}
I checked other questions on here, but people are usually (in my eyes) lost in some details which I don't understand. Any help would be very much appreciated! Thanks for your patience.
You can simply
UITextView *theview; //remove this line, and change future theview to your veiw
NSString *thestring; //for storing a string from your view
int returnint = 0;
thestring = [NSString stringWithFormat:#"%#",[theview text]];
for (int temp = 0; temp < [thestring length]; temp++){ //run through the string
if ([thestring characterAtIndex: temp] == '\n')
returnint++;
}
NSArray *newlines = [textView.text componentsSeparatedByString:#"\n"];
int returns = ([newlines count]-1)
Should work. Keep in mind this isn't such a great idea if you have a gia-normous string, but it's quick, dirty and easy to implement.
there are a lot of ways to do that. Here is one:
NSString *str = #"FooBar\n\nBaz...\n\nABC\n";
NSString *tmpStr = [str stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSInteger count = [str length] - [tmpStr length];
NSLog(#"Count: %d", count);

getting the same value in each cell of table view in objective-c

i have check by printing the value of the cell.textlable.text in each iteration of the loop each time the value of it is as i want, but as the time of output on the simulator the value is come same for each row of cell.
my code:
for(i=0;i<[appDelegate.books count];i++)
{
Book *aBook= [appDelegate.books
objectAtIndex:i];
NSString *string1 =aBook.Latitude;
NSString *trimmedString = [string1
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
double myDouble = [trimmedString doubleValue];
NSString *string2=aBook.Longitude;
NSString *trimmedString1 = [string2
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]];
double mDouble = [trimmedString1 doubleValue];
if((((myDouble<(a+5.021777))&&(myDouble>(a-
8)))||((mDouble<=(b+10))&&(mDouble>=(b-10)))))
{
NSString *a1=aBook.AreaName;
NSString *b1=[a1 stringByAppendingString:#","];
NSString *s=aBook.STREET_NAME;
NSString *c=[b1 stringByAppendingString:s];
cell.textLabel.text = c ;
}
}
return cell; }
The problem is likely this line:
for(i=0;i<[appDelegate.books count];i++)
That's going to loop through every item in appDelegate.books. Assuming that that's an NSArray and that this code is inside your -tableView:cellForRowAtIndexPath: method, replace that line and the next with the following:
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
(This, of course, assumes that the row numbers match the index numbers in the array. If not, adjust to match.)