How to get a NSRange Value from NSConcreteValue? - ios

The code is like this. My question is how to get the NSRange value from the id type?
-(void)clicText:(MyLabel *)label clickedOnLink:(id)linkData{
NSString *message = [NSString stringWithFormat:#"LinkData is %#:%#",[[linkData class] description],linkData];
}
I get the type is NSConcreteValue and the data value is NSRange:{0,4}; but how do I get the NSRange from the NSConcreteValue?
I have already tried the [NSValue valueWithNonretainedObject:linkData]; but it does not make sense.

Use the rangeValue method:
NSRange range = [linkData rangeValue]; // assume linkData is NSValue

Related

How to remove conflicting return type?

I am dividing string and and storing it in splitArray and want to return it.
But I am getting conflicting array on the first line
- (NSArray *)subdividingString:(NSString *)string {
NSArray *splitArray = [string componentsSeparatedByString:#" "];
return splitArray;
}
First: there is nothing wrong with the code, but you are most likely having another issue (e.g. where you call subdividingString:).
Second: You shouldn't introduce a method that is exactly doing what another one is doing already. Just use
NSString *mystring = #"some string";
NSArray *chunks = [mystring componentsSeparatedByString:#" "];

Replace String Between Two Strings

I have a serious problem about indexing in array. I've been working on this for 2 days and couldn't find answer yet.
I want to do that, search specific character in array then replace it with other string. I'm using replaceObjectAtIndex method but my code is doesn't work.
Here is my code;
NSString *commentText = commentTextView.text;
NSUInteger textLength = [commentText length];
NSString *atSign = #"#";
NSMutableArray *commentArray = [[NSMutableArray alloc] init];
[commentArray addObject:commentText];
for (int arrayCounter=1; arrayCounter<=textLength; arrayCounter++)
{
NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch];
if(isRange.location != NSNotFound)
{
commentText = [commentText stringByReplacingOccurrencesOfString:commentText withString:atSign];
[_mentionsearch filtrele:_mentionText];
id<textSearchProtocol> delegate;
[delegate closeList:[[self.searchResult valueForKey:#"user_name"] objectAtIndex:indexPath.row]];
}
}
Ok, now i can find "#" sign in the text and i can match it. But this is the source of problem that, i can not replace any string with "#" sign. Here is the last part of code;
-(void)closeList
{
NSArray *arrayWithSign = [commentTextView.text componentsSeparatedByString:#" "];
NSMutableArray *arrayCopy = [arrayWithSign mutableCopy];
[arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:#"#%#",username]];
}
When im logging isRange.location value, it returns correct. But when im try to run, my application is crashing. So, i can not replacing [NSString stringWithFormat:#"#%#",username] parameter. How can i solve this problem?
If I understand correctly you want to change a substring in a string with a new string. In this case, why don't you use directly the stringByReplacingOccurrencesOfString method of NSString:
NSString *stringToBeChanged = #"...";
NSString *stringToBeChangedWith = #"...";
NSString *commentTextNew = [commentText stringByReplacingOccurrencesOfString:stringToBeChanged withString:stringToBeChangedWith];

iOS: changing NSString value

Will this bit of code produce any memory leaks? Is it the correct way to change NSString values?
NSString * enemiesAndElementsTextureFileName = #"bla bla";
enemiesAndElementsTextureFileName = #"bl";
That way of doing it won't cause any memory leaks and it is indeed correct. In this case you wouldn't need an NSMutableString because you aren't altering the string literal itself, you are simply replacing the string value with a new one (replacing #"bla bla" with #"bl").
In this case, however, your string will now be 'bl', so you can delete that first line value and just have NSString * enemiesAndElementsTextureFileName = #"bl";
Yes NSString allocated once. This is one of the way
Yes, use NSMutableString with the following method as your needs:
// Allocate
NSMutableString *str = [[NSMutableString alloc] initWithCapacity:10];
// set string content
[str setString:#"1234"];
// Append
[str appendString:#"567"];
// Concat
[str appendFormat:#"age is %i and height is %.2f", 27, 1.55f];
// Replace
NSRange range = [str rangeOfString:#"height"];//查找字符串height的位置
[str replaceCharactersInRange:range withString:#"no"];
// Insert
[str insertString:#"abc" atIndex:2];
// Delete
range = [str rangeOfString:#"age"];
[str deleteCharactersInRange:range];
NSLog(#"%#", str);

Cut NSString from space to end

I have NSString:
sessid=os3vainreuru2hank3; __ubic1=MzcxMzjMDYuNjk0NDA1Mzc%3D;
auto_login=123; sid=kep8efpo7; last_user=123;
I need get just:
__ubic1=MzcxMzjMDYuNjk0NDA1Mzc%3D; auto_login=123;
interpals_sessid=kep8efpo7; last_user=123;
But count of characters past sessid may vary
Thanks! Sorry for simple question
This should do the trick.-
NSRange range = [yourString rangeOfString:#" "];
if (NSNotFound != range.location) {
yourString = [yourString substringFromIndex:(range.location + 1)];
}
Basically, you get the index for the first space character, and then the substring from that index to the end.
You'll need at least one character you can search for. Looks like that double underscore will work.
NSRange stringStart = [originalString rangeOfString:#"__"];
NSString *extractedString = [originalString substringWithRange:NSMakeRange(stringStart.location, originalString.length - stringStart.location)];
That should get you what you need!
NSMutableArray* array = [[originalString componentsSeperatedByString:#";"] mutableCopy];
[array removeObjectAtIndex:0];
NSString* newString = [array componentsJoinedByString:#";"];
I assume you mistyped interpals_sessid with sid

String compare in iOS

I want to find out whether a given NSString or CFStringRef contains a certain substring. How do I do that?
- (NSRange)rangeOfString:(NSString *)aString
...is your man. For example:
NSString* search = #"Where's the manual?";
NSRange range = [search rangeOfString: #"the"];
NSLog( #"Found at %u", range.location );
Please read the NSString class reference. What you want is pathExtension.

Resources