 |
 |
determining if a string contains a character
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
I need to determine if a string contains a certain character. Right now Im doing the following which does NOT work. could someone point me in the right direction?
if ([[dict objectForKey:@"overallmaxspeeddaydays"] doesContain:@"M"]) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}
tia
|
3R1C
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
See the headers or documentation for -[NSString rangeOfString:].
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
And if you're too lazy to read the headers...here's a nice category you can add to NSString:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@implementation NSString (NSStringAdditions)
- (BOOL)containsString  NSString *)aString
{
return [self rangeOfString:aString].location != NSNotFound;
}
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">then just do:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([[dict objectForKey:@"overallmaxspeeddaydays& ;quot;] containsString:@"M"])
{
[dayMon setIntValue:1];
}
else
{
[dayMon setIntValue:0];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
Thanx Rick, but the docs dont really help me in this instance. Does anyone have/know of any code which does this? I need to look at code to see how it works. Apples docs rarely help me (not apples fault, im just slow).
wow thanks ibson. that not only solved my problem but showed me how to use rangeOfString.
<small>[ 07-10-2002, 09:17 PM: Message edited by: 3R1C ]</small>
|
3R1C
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Thanx Rick, but the docs dont really help me in this instance. Does anyone have/know of any code which does this? I need to look at code to see how it works. Apples docs rarely help me (not apples fault, im just slow).</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Look above  .
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
If you're not familiar with categories, you would also create a header file looking like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@interface NSString (NSStringAdditions)
- (BOOL)containsString  NSString *)aString;
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and import that into your other source file.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:
if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}
I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?
|
3R1C
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:
if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}
I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well done  . A category extends a class; it is no faster, and no more efficient--unless it is a large block of code (therefore it isn't being needlessly repeated). A category provides a convenient way to add (or alter) a method to a class, so that it can be used anywhere. For example, if you used the category I posted earlier, you would be able to send *any string* a containsString: message, instead of writing out rangeOfString, location, and NSNotFound. So, you could re-write the above code block as:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([[dict objectForKey:@"overallmaxspeeddaydays& ;quot;] containsString:@"M"])
...</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">...which is far more readable. Then, somewhere else in your application (it could be the next line, or a completely different method in another source file) you could write:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">if ([@"A horse is a horse, of course, of course." containsString:@"frog"])....</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">...which of course saves you from re-writing the rangeOfString: code block.
To provide this functionality, you would create a file named "NSString+Additions.h", which would contain:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#import <Foundation/Foundation.h>
@interface NSString (NSStringAdditions)
- (BOOL)containsString  NSString *)aString;
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and then "NSString+Additions.m", containing:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#import "NSString+Additions.h"
@implementation NSString (NSStringAdditions)
- (BOOL)containsString  NSString *)aString
{
return [self rangeOfString:aString].location != NSNotFound;
}
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Then, at the beginning of the source file containing your other code, you would import NSString+Additions.h, meaning you could send any string a containsString: message, and it would work.
Another popular (yet simplistic) use of categories is to allow any array or any dictionary to become a table view's datasource. So, you could have something like:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSArray *myArray = ...;
[myTableView setDataSource:myArray];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and the category would do the rest. I did something like this for Synotic a while ago; search the forums if you are interested.
I hope this (rather long) explanation helps  .
<small>[ 07-10-2002, 10:00 PM: Message edited by: Ibson ]</small>
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by 3R1C:
<strong>Just to show you that I want to learn this stuff and not just have you guys write me blocks of code, I used what you taught me to come up with this:
if ([[dict objectForKey:@"overallmaxspeeddaydays"] rangeOfString:@"M"].location != NSNotFound) {
[dayMon setIntValue:1];
} else {
[dayMon setIntValue:0];
}
I will now attempt to learn these 'categories'. When I get the category to work will it be faster? More efficient?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Well, the category will actually be insignificantly slower (since you call the -containsString: method, and then it performs the -rangeOfString: test). But it will be more convenient, because all you have to do is import the category and you can use the -containsString: method anytime you'd like to tell whether or not a string contains a substring.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Categories are explained in detail in the book "Object-Oriented Programming and the Objective-C Language", available <a href="http://developer.apple.com/techpubs/macosx/Cocoa/ObjectiveC/ObjC.pdf" target="_blank">here</a> (also available in <a href="http://catalog.vervante.com/viewProduct.cfm?item_id=607511" target="_blank">hardcopy</a>).
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Angus_D:
<strong>Categories are explained in detail in the book "Object-Oriented Programming and the Objective-C Language", available <a href="http://developer.apple.com/techpubs/macosx/Cocoa/ObjectiveC/ObjC.pdf" target="_blank">here</a> (also available in <a href="http://catalog.vervante.com/viewProduct.cfm?item_id=607511" target="_blank">hardcopy</a>).</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Also available on your hard drive at /Developer/Documentation/Cocoa/ObjectiveC/ in both PDF and HTML foms, FYI.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|