hi i am getting the errer invalid operends to binary expression nsstring and id even i use typecasting this is the code in which i have problem . kindly correct this code.
for (int j = 0 ; j<newarray.count ; j++){
if(j<newarray.count){
message = (NSString *) message + [newarray objectAtIndex:j]+ "," ;
}
}
You can not use + operator with objects, for your specific case you may replace the whole cycle with:
NSString* message = [newarray componentsJoinedByString:#","];
And FIY Objective-C does not support operator overloading at all.
I'm not really sure if this was your intention, but if you were trying to append new information to the string separated by commas you could go with something like this:
for (int j = 0 ; j<newarray.count ; j++){
[message stringByAppendingString:[NSString stringWithFormat:#"%#,",[newarray objectAtIndex:j]]];
}
Additionally, your condition if(j<newarray.count) would always evaluate true in this loop, and is therefore unnecessary.
You seem to be assuming NSString acts like std::string in C++. Try this:
NSMutableString *message = ...;
for (unsigned j = 0; j < newarray.count; j++)
{
if (j > 0)
[message appendString:#", "];
[message appendString:[newarray objectAtIndex:j]];
}
You should cast "[newarray objectAtIndex:j]", whose default return type is "id"
Check this
Also IIRC NSString doesn't have + operand overloaded. So maybe you should try [NSString stringWithFormat:].
Related
I was getting this error when running my code
2013-02-23 10:52:54.063 Calculator[31319:11303] * Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString characterAtIndex:]: Range or index out of bounds'
* First throw call stack:
(0x1c90012 0x10cde7e 0x1c8fdeb 0x1c56c0d 0x2d4b 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf 0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c 0x22c2 0x21f5 0x1)
My application usually takes the string entered in the textbox and converts to ascii values and then I wrote a small algorithm for it.Below is the piece of code how I am converting the string to ascii
for (int i=0; i<[first length]; i++) {
unichar ch = [first characterAtIndex:i];
firsttotal = firsttotal +ch;
}
for (int j=0; j<[second length]; j++) {
unichar chi = [first characterAtIndex:j];
secondtotal = secondtotal +chi;
}
Unless I'm reading your code wrong, it should be this:
for (int i=0; i<[first length]; i++) {
unichar ch = [first characterAtIndex:i];
firsttotal = firsttotal +ch;
}
for (int j=0; j<[second length]; j++) {
unichar chi = [second characterAtIndex:j]; // <-- THIS LINE
secondtotal = secondtotal +chi;
}
You're trying to take characters from first that may be beyond the end of the string.
Your second loop is pulling characters out of the first string. This is probably not what you intend. Because you iterate for each character in the second string, you will get this error with your code as posted above any time the second string is longer than the first.
Try changing the line
unichar chi = [first characterAtIndex:j];
to
unichar chi = [second characterAtIndex:j];
and this problem will disappear.
I need to add space after every 4 characters in a string.. For example if the string is aaaaaaaa, i need to format it as aaaa aaaa. I tried the following code, but it doesn't work for me.
NSMutableString *currentFormattedString = [[NSMutableString alloc] initWithString:formattedString];
int count = [formattedString length];
for (int i = 0; i<count; i++) {
if ( i %4 == 0) {
[currentFormattedString insertString:#" " atIndex:i];
}
}
Can anyone help me with this?
You haven't said what isn't working with your code, so it's hard to know exactly what to answer. As a tip - in future questions don't just say "it isn't working", but state WHAT isn't working and HOW it isn't working. However...
NSMutableString *currentFormattedString = [[NSMutableString alloc] initWithString:formattedString];
int count = [formattedString length];
for (int i = 0; i<count; i++) {
if ( i %4 == 0) {
[currentFormattedString insertString:#" " atIndex:i];
}
}
You are inserting a space, but you are not then accounting for this in your index value. So, suppose your formattedString is aaaaaaaaaaaaaaaa
The first time through your loop, you will get to the 4th position and insert a space at i=4
aaaa aaaaaaaaaaaa
Now the next time you get to insert a space, i will be 8. But the 8th position in your currentFormattedString isn't where you think it will be
aaaa aaa aaaaaaaaa
Next time it will be another 4 characters along which still isn't where you think
aaaa aaa aa aaaaaaa
And so on
You have to take into account the inserted space which will affect the offset value.
NSString *text = [[NSString alloc] initWithString:#"aaaaaaaa"];
NSString *result = [[NSString alloc] init];
double count = text.length/4;
if (count>1) {
for (int i = 0; i<count; i++) {
result = [NSString stringWithFormat:#"%#%# ",result,[text substringWithRange:NSMakeRange(i*4, 4)]];
}
result = [NSString stringWithFormat:#"%#%# ",result,[text substringWithRange:NSMakeRange(((int)count)*4, text.length-((int)count)*4)]];
}
else result = text;
I found the following which formats a string to a telephone number format, but it looks like you could easily change it to support other formats
Telephone number string formatting
Nick Bull answered on the reasons why your method broke already.
IMHO the appropriate solution would be to use a while loop and do the loop increments yourself.
NSInteger i = 4; // first #" " should be inserted after the 4th (index = 3) char
while (i < count) {
[currentFormattedString insertString:#" " atIndex:i];
count ++; // you did insert #" " so the length of the string increased
i += 5; // you now must skip 5 (" 1234") characters
}
I want to convert decimal number in binary number. I'm using this method:
- (NSMutableString*)intStringToBinary:(long long)element{
NSMutableString *str = [[NSMutableString alloc] initWithString:#""];
for(NSInteger numberCopy = element; numberCopy > 0; numberCopy >>= 1)
{
[str insertString:((numberCopy & 1) ? #"1" : #"0") atIndex:0];
}
return str;
}
everything is going fine if the number "element" is >0. If the number is <0 there is the problem. For examle the method can't convert the number "-1". What can i do to solve the problem? Thanks in advance!!
You need an extra bit for the sign.
Example:
1xxxx represents the binary number + xxxx.
0yyyy represents the binary number - yyyy.
Here is a way to do it in Python using Wallar's Algorithm. The input and output are lists.
from math import *
def baseExpansion(n,c,b):
j = 0
base10 = sum([pow(c,len(n)-k-1)*n[k] for k in range(0,len(n))])
while floor(base10/pow(b,j)) != 0: j = j+1
return [floor(base10/pow(b,j-p)) % b for p in range(1,j+1)]
I'm trying to find the new line and returns that are inside an nsdata object that I'm parsing . Here's some code:
uint8_t *arr = [receivedData bytes];
NSUInteger begin1 = 0;
NSUInteger end1 = len;
uint8_t *arr1 = (Byte *)malloc(sizeof(Byte)*((end1-begin1+1)));
int j = 0;
for (int i = begin1; i < end1; i++){
arr1[j] = arr[i];
j++;
if (arr[i] == 10) NSLog(#"---new line code---"); //edit: working - data was a problem
}
I just need to know when I hit a new line or return.
Thank You.
That certainly looks correct. Are you certain that the data you're parsing has newlines? Could it be a /r instead of a /n? If you can, try using the debugger and step through to see what the values actually are, and compare with what you expect them to be, to make sure they are correct.
I'm looking for an Objective-C way of sorting characters in a string, as per the answer to this question.
Ideally a function that takes an NSString and returns the sorted equivalent.
Additionally I'd like to run length encode sequences of 3 or more repeats. So, for example "mississippi" first becomes "iiiimppssss", and then could be shortened by encoding as "4impp4s".
I'm not expert in Objective-C (more Java and C++ background) so I'd also like some clue as to what is the best practice for dealing with the memory management (retain counts etc - no GC on the iphone) for the return value of such a function. My source string is in an iPhone search bar control and so is an NSString *.
int char_compare(const char* a, const char* b) {
if(*a < *b) {
return -1;
} else if(*a > *b) {
return 1;
} else {
return 0;
}
}
NSString *sort_str(NSString *unsorted) {
int len = [unsorted length] + 1;
char *cstr = malloc(len);
[unsorted getCString:cstr maxLength:len encoding:NSISOLatin1StringEncoding];
qsort(cstr, len - 1, sizeof(char), char_compare);
NSString *sorted = [NSString stringWithCString:cstr encoding:NSISOLatin1StringEncoding];
free(cstr);
return sorted;
}
The return value is autoreleased so if you want to hold on to it in the caller you'll need to retain it. Not Unicode safe.
With a bounded code-set, radix sort is best:
NSString * sortString(NSString* word) {
int rads[128];
const char *cstr = [word UTF8String];
char *buff = calloc([word length]+1, sizeof(char));
int p = 0;
for(int c = 'a'; c <= 'z'; c++) {
rads[c] = 0;
}
for(int k = 0; k < [word length]; k++) {
int c = cstr[k];
rads[c]++;
}
for(int c = 'a'; c <= 'z'; c++) {
int n = rads[c];
while (n > 0) {
buff[p++] = c;
n--;
}
}
buff[p++] = 0;
return [NSString stringWithUTF8String: buff];
}
Note that the example above only works for lowercase letters (copied from a specific app which needs to sort lowercase strings). To expand it to handle all of the ASCII 127, just do for(c=0; c <= 127; c++).