Compare String Url with a String value - iphone

How can I search a string url for a string value?
String url:
http://localHost:8070/serviceCase=ActiveService
http://localHost:8070/serviceCase=KoplService
String Value is:
ActiveService
I need to print the urls that do have "ActiveService"!

You could probably do something like this(if your url really is a string):
if ([url rangeOfString:value].location == NSNotFound){
dosomething
} else {
doSomethingElse
}

NSString *url = #"http://localHost:8070/serviceCase=ActiveService";
if ([url rangeOfString:#"ActiveService"].location != NSNotFound) {
NSLog(#"URL has the string");
}

I recommend below NSString Category.
NSString+Extend.h
#import <Foundation/Foundation.h>
#interface NSString (Extend)
- (BOOL)containsString:(NSString *)aString ignoringCase:(BOOL)flag;
- (BOOL)containsString:(NSString *)aString;
#end
NSString+Extend.m
#import "NSString+Extend.h"
#implementation NSString (Extend)
- (BOOL)containsString:(NSString *)aString
{
return [self containsString:aString ignoringCase:NO];
}
- (BOOL)containsString:(NSString *)aString ignoringCase:(BOOL)flag
{
unsigned mask = (flag ? NSCaseInsensitiveSearch : 0);
return [self rangeOfString:aString options:mask].length > 0;
}
#end
#import "NSString+Extend.h"
NSString *url = #"http://localHost:8070/serviceCase=ActiveService";
NSString *findString = #"ActiveService";
BOOL isContains = [url containsString:findString];
if(isContains)
{
//do stuff
}
else
{
//do stuff
}

if([string hasSuffix:#"ActiveService"]) {
// Do your stuff
}

Related

No known class method for selector 'myStaticUsername'

So I'm trying to access the variable 'username' from another class and append it to a URL as a string. I'm getting the error "No known class method for selector 'myStaticUsername'" and don't really know how to solve it. Any help would be appreciated.
FirstViewController.h
+(NSString *) username;
FirstViewController.m
static NSString *myStaticUsername = nil;
#implementation FirstViewController
+(NSString *) username{
return myStaticUsername;
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Cancel Tapped.");
}
else if (buttonIndex == 1) {
myStaticUsername= Textbox.text;
NSLog(myStaticUsername);
}
My ViewController.m class where I'm trying to access it:
- (void)viewDidLoad
{
NSString *email = [FirstViewController myStaticUsername];
NSString *website =[NSString stringWithFormat:#"http://www.nameofwebsite?un=:%#", email]; //append the username right here
Thanks.
In your viewDidLoad you need to change this:
NSString *email = [FirstViewController myStaticUsername];
to this:
NSString *email = [FirstViewController username];
myStaticUsername is the name of your static NSString, but the name of the class method you wrote to access it is username.

What alternatives to a switch statement could I use to update my UITableViewCells? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Alternative to switch statement in objective C
I have json data from an url which I display in the table cell using a switch statement. Since I have only 6 cells, I had used a switch statement but I am curious to know if there is any other method in place of switch statement to do so.
switch(indexPath.row)
{
case 0 :
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",[dictionary valueForKey:#"firstname"],
[dictionary valueForKey:#"lastname"]];
break;
case 1:
cell.textLabel.text = [NSString stringWithFormat:#"%# : %#",#"Address",[dictionary valueForKey:#"address"]];
break;
case 2:
cell.textLabel.text = [NSString stringWithFormat:#"%# : %#",#"Age",[dictionary valueForKey:#"age"]];
break;
case 3:
cell.textLabel.text = [NSString stringWithFormat:#"%# : %#",#"Occupation",[dictionary valueForKey:#"occupation"]];
break;
case 4:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NSString stringWithFormat:#"%# : %#",#"Rating",[rate valueForKey:#"average"]];
break;
case 5:
cell.textLabel.text = [NSString stringWithFormat:#"%# : %#",#"Total Rating",[rate valueForKey:#"totalRatings"]];
break;
}
Here's a way to completely overengineer the problem, but you may find some useful nuggets in there which will help you with your specific problem:
typedef enum MONTableViewCellID {
MONTableViewCellID_Name = 0,
MONTableViewCellID_Address,
MONTableViewCellID_Age,
MONTableViewCellID_Occupation,
MONTableViewCellID_Rating,
MONTableViewCellID_TotalRating
} MONTableViewCellID;
#interface MONTableViewStuff : NSObject
{
#private
NSDictionary * dictionary;
NSDictionary * rate;
UITableViewCell * cell;
NSIndexPath * indexPath;
}
#end
#implementation MONTableViewStuff
- (UITableViewCellAccessoryType)tableViewAccessoryTypeForRow:(NSUInteger)row
{
if (MONTableViewCellID_Rating == row) {
return UITableViewCellAccessoryDisclosureIndicator;
}
return UITableViewCellAccessoryNone;
}
- (NSString *)lhsTextForRow:(NSUInteger)row
{
switch (row) {
case MONTableViewCellID_Name :
return [dictionary objectForKey:#"firstname"];
case MONTableViewCellID_Address :
return #"Address";
case MONTableViewCellID_Age :
return #"Age";
case MONTableViewCellID_Occupation :
return #"Occupation";
case MONTableViewCellID_Rating :
return #"Rating";
case MONTableViewCellID_TotalRating :
return #"Total Rating";
default : {
assert(0 && "invalid row");
return #"";
}
}
}
- (NSString *)rhsTextForRow:(NSUInteger)row
{
switch (row) {
case MONTableViewCellID_Name :
return [dictionary objectForKey:#"lastname"];
case MONTableViewCellID_Address :
return [dictionary objectForKey:#"address"];
case MONTableViewCellID_Age :
return [dictionary objectForKey:#"age"];
case MONTableViewCellID_Occupation :
return [dictionary objectForKey:#"occupation"];
case MONTableViewCellID_Rating :
return [rate objectForKey:#"average"];
case MONTableViewCellID_TotalRating :
return [rate objectForKey:#"totalRatings"];
default : {
assert(0 && "invalid row");
return #"";
}
}
}
- (NSString *)separatorForRow:(NSUInteger)row
{
switch (row) {
case MONTableViewCellID_Name :
return #" ";
case MONTableViewCellID_Address :
case MONTableViewCellID_Age :
case MONTableViewCellID_Occupation :
case MONTableViewCellID_Rating :
case MONTableViewCellID_TotalRating :
return #" : ";
default : {
assert(0 && "invalid row");
return #"";
}
}
}
- (NSString *)textLabelTextForRow:(NSUInteger)row
{
return [NSString stringWithFormat:#"%#%#%#", [self lhsTextForRow:row], [self separatorForRow:row], [self rhsTextForRow:row]];
}
- (void)updateTextLabel
{
cell.textLabel.text = [self textLabelTextForRow:indexPath.row];
cell.accessoryType = [self tableViewAccessoryTypeForRow:indexPath.row];
}
#end
Another overengineered option that you may be able to steal some nuggets from is the object based approach:
MONTableViewStuff.h
typedef enum MONTableViewCellID {
MONTableViewCellID_Name = 0,
MONTableViewCellID_Address,
MONTableViewCellID_Age,
MONTableViewCellID_Occupation,
MONTableViewCellID_Rating,
MONTableViewCellID_TotalRating
} MONTableViewCellID;
#interface MONTableViewStuff : NSObject
#property (nonatomic, copy, readonly) NSDictionary * dictionary;
#property (nonatomic, copy, readonly) NSDictionary * rate;
#end
MONTableViewStuff.m
#implementation MONTableViewStuff
#synthesize dictionary;
#synthesize rate;
- (id)init
{
self = [super init];
if (0 != self) {
/* create an array of presenters ordered by MONTableViewCellID */
presenters =
[NSArray arrayWithObjects:
[[NamePresenter new] autorelease],
[[AddressPresenter new] autorelease],
[[AgePresenter new] autorelease],
[[OccupationPresenter new] autorelease],
[[RatingPresenter new] autorelease],
[[TotalRatingPresenter new] autorelease],
nil
];
}
return self;
}
- (void)updateTableViewCell
{
NSObject<MONUITableViewCellPresenter>* presenter = [presenters objectAtIndex:indexPath.row];
[presenter updateUITableViewCell:cell tableViewStuff:self];
}
#end
Where the presenters' interface looked like so:
#protocol MONUITableViewCellPresenter < NSObject >
#required
- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff;
#end
// our base presenter which handles the cells
#interface DefaultPresenter : NSObject
/** #return UITableViewCellAccessoryNone */
- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff;
- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff;
#end
// required overrides
#protocol DefaultPresenterSubclass <MONUITableViewCellPresenter>
#required
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff;
#end
// our specializations
#interface NamePresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
#interface AddressPresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
#interface AgePresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
#interface OccupationPresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
#interface RatingPresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
#interface TotalRatingPresenter : DefaultPresenter <DefaultPresenterSubclass>
#end
And their implementations looked like so:
#implementation DefaultPresenter
- (UITableViewCellAccessoryType)cellAccessoryTypeForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
#pragma unused (tableViewStuff)
return UITableViewCellAccessoryNone;
}
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
#pragma unused (tableViewStuff)
assert(0 && "specialization required");
return 0;
}
- (void)updateUITableViewCell:(UITableViewCell *)cell tableViewStuff:(MONTableViewStuff *)tableViewStuff
{
cell.accessoryType = [self cellAccessoryTypeForTableViewStuff:tableViewStuff];
cell.textLabel.text = [self cellTextForTableViewStuff:tableViewStuff];
}
#end
#implementation NamePresenter
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# %#",[tableViewStuff.dictionary valueForKey:#"firstname"], [tableViewStuff.dictionary valueForKey:#"lastname"]];
}
#end
#implementation AddressPresenter
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# : %#",#"Address",[tableViewStuff.dictionary valueForKey:#"address"]];
}
#end
#implementation AgePresenter
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# : %#",#"Age",[tableViewStuff.dictionary valueForKey:#"age"]];;
}
#end
#implementation OccupationPresenter
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# : %#",#"Occupation",[tableViewStuff.dictionary valueForKey:#"occupation"]];
}
#end
#implementation RatingPresenter
+ (UITableViewCellAccessoryType)cellAccessoryType
{
return UITableViewCellAccessoryDisclosureIndicator;
}
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# : %#",#"Rating",[tableViewStuff.rate valueForKey:#"average"]];
}
#end
#implementation TotalRatingPresenter
- (NSString *)cellTextForTableViewStuff:(MONTableViewStuff *)tableViewStuff
{
return [NSString stringWithFormat:#"%# : %#",#"Total Rating",[tableViewStuff.rate valueForKey:#"totalRatings"]];
}
#end
The thing you could do would be to make some kind of data objet containing the information you need to display in your stringWithFormat method.
And then make a NSDictionary that key value pair those object with the indexes...
But is all that work worth it in this particular case?
I doubt it.

Retrieving a filename for an ALAsset

How can the filename be extracted from an ALAsset?
Is there a way to get this via the url or some other way?
From iOS 5.0 you can get the file from ALAssetRepresentation Class.
ALAssetRepresentation *rep = [anAssetItem defaultRepresentation];
NSString *fileName = [rep filename];
Update: As yeonsh notes below, from iOS 5.0 there is a better way. This answer is relevant for iOS < 5.0.
You can extract an URL from the ALAsset, but all the filenames are the same, on the form
assets-library://asset/asset.JPG?id=1000000001&ext=JPG
If you for some reason need different file names, try making an internal-external paradigm:
#import <Foundation/Foundation.h>
#interface NSURL (NSURL_Asset)
- (NSURL*) toExternalForm;
- (NSURL*) fromExternalForm;
- (NSString*) toExternalFilename;
#end
#import "NSURL+Asset.h"
#import "URLParser.h" // from http://iphone.demay-fr.net/2010/04/parsing-url-parameters-in-a-nsstring/
static NSString *const EXTERNAL_TOKEN = #"/assetExternalForm/";
#implementation NSURL (NSURL_Asset)
// assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG -> assets-library://asset/asset.JPG?id=1000000001&ext=JPG
- (NSURL*) fromExternalForm {
if([self.scheme isEqualToString:#"assets-library"]) {
NSRange slash = [self.absoluteString rangeOfString:EXTERNAL_TOKEN options:NSBackwardsSearch];
if(slash.location != NSNotFound) {
NSRange dot = [self.absoluteString rangeOfString:#"." options:NSBackwardsSearch];
if(dot.location != NSNotFound) {
NSString* extention = [self.absoluteString substringFromIndex:(dot.location + dot.length)];
NSString* identifier = [self.absoluteString substringWithRange:NSMakeRange(slash.location + slash.length, dot.location - (slash.location + slash.length))];
return [NSURL URLWithString:[NSString stringWithFormat:#"%#?id=%#&ext=%#", [self.absoluteString substringToIndex:slash.location], identifier, extention]];
}
}
}
return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> assets-library://asset/asset.JPG/assetExternalForm/1000000001.JPG
- (NSURL*) toExternalForm {
if([self.scheme isEqualToString:#"assets-library"]) {
NSRange range = [self.absoluteString rangeOfString:#"?"];
if(range.location != NSNotFound) {
URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
NSString* extention = [parser valueForVariable:#"ext"];
NSString* identifier = [parser valueForVariable:#"id"];
if(extention != NULL && identifier != NULL) {
return [NSURL URLWithString:[NSString stringWithFormat:#"%#%#%#.%#", [self.absoluteString substringToIndex:range.location], EXTERNAL_TOKEN, identifier, extention]];
}
}
}
return self;
}
// assets-library://asset/asset.JPG?id=1000000001&ext=JPG -> 1000000001.JPG
- (NSString*) toExternalFilename {
if([self.scheme isEqualToString:#"assets-library"]) {
NSRange range = [self.absoluteString rangeOfString:#"?"];
if(range.location != NSNotFound) {
URLParser *parser = [[[URLParser alloc] initWithURLString:self.absoluteString] autorelease];
NSString* extention = [parser valueForVariable:#"ext"];
NSString* identifier = [parser valueForVariable:#"id"];
if(extention != NULL && identifier != NULL) {
return [NSString stringWithFormat:#"%#.%#", identifier, extention];
}
}
}
return NULL;
}
#end
Note that you do not need a filename to read the content of an ALAsset. Use the ALAsset.defaultRepresentation.getBytes method for that.

regular expression to read the string between <title> and </title>

I hope to read the contents between and in a html string.
I think it should be in objective-c
#"<title([\\s\\S]*)</title>"
below are the codes that rewrited for regular expression
//source of NSStringCategory.h
#import <Foundation/Foundation.h>
#import <regex.h>
#interface NSStringCategory:NSObject
{
regex_t preg;
}
-(id)initWithPattern:(NSString *)pattern options:(int)options;
-(void)dealloc;
-(BOOL)matchesString:(NSString *)string;
-(NSString *)matchedSubstringOfString:(NSString *)string;
-(NSArray *)capturedSubstringsOfString:(NSString *)string;
+(NSStringCategory *)regexWithPattern:(NSString *)pattern options:(int)options;
+(NSStringCategory *)regexWithPattern:(NSString *)pattern;
+(NSString *)null;
+(void)initialize;
#end
#interface NSString (NSStringCategory)
-(BOOL)matchedByPattern:(NSString *)pattern options:(int)options;
-(BOOL)matchedByPattern:(NSString *)pattern;
-(NSString *)substringMatchedByPattern:(NSString *)pattern options:(int)options;
-(NSString *)substringMatchedByPattern:(NSString *)pattern;
-(NSArray *)substringsCapturedByPattern:(NSString *)pattern options:(int)options;
-(NSArray *)substringsCapturedByPattern:(NSString *)pattern;
-(NSString *)escapedPattern;
#end
and .m file
#import "NSStringCategory.h"
static NSString *nullstring=nil;
#implementation NSStringCategory
-(id)initWithPattern:(NSString *)pattern options:(int)options
{
if(self=[super init])
{
int err=regcomp(&preg,[pattern UTF8String],options|REG_EXTENDED);
if(err)
{
char errbuf[256];
regerror(err,&preg,errbuf,sizeof(errbuf));
[NSException raise:#"CSRegexException"
format:#"Could not compile regex \"%#\": %s",pattern,errbuf];
}
}
return self;
}
-(void)dealloc
{
regfree(&preg);
[super dealloc];
}
-(BOOL)matchesString:(NSString *)string
{
if(regexec(&preg,[string UTF8String],0,NULL,0)==0) return YES;
return NO;
}
-(NSString *)matchedSubstringOfString:(NSString *)string
{
const char *cstr=[string UTF8String];
regmatch_t match;
if(regexec(&preg,cstr,1,&match,0)==0)
{
return [[[NSString alloc] initWithBytes:cstr+match.rm_so
length:match.rm_eo-match.rm_so encoding:NSUTF8StringEncoding] autorelease];
}
return nil;
}
-(NSArray *)capturedSubstringsOfString:(NSString *)string
{
const char *cstr=[string UTF8String];
int num=preg.re_nsub+1;
regmatch_t *matches=calloc(sizeof(regmatch_t),num);
if(regexec(&preg,cstr,num,matches,0)==0)
{
NSMutableArray *array=[NSMutableArray arrayWithCapacity:num];
int i;
for(i=0;i<num;i++)
{
NSString *str;
if(matches[i].rm_so==-1&&matches[i].rm_eo==-1) str=nullstring;
else str=[[[NSString alloc] initWithBytes:cstr+matches[i].rm_so
length:matches[i].rm_eo-matches[i].rm_so encoding:NSUTF8StringEncoding] autorelease];
[array addObject:str];
}
free(matches);
return [NSArray arrayWithArray:array];
}
free(matches);
return nil;
}
+(NSStringCategory *)regexWithPattern:(NSString *)pattern options:(int)options
{ return [[[NSStringCategory alloc] initWithPattern:pattern options:options] autorelease]; }
+(NSStringCategory *)regexWithPattern:(NSString *)pattern
{ return [[[NSStringCategory alloc] initWithPattern:pattern options:0] autorelease]; }
+(NSString *)null { return nullstring; }
+(void)initialize
{
if(!nullstring) nullstring=[[NSString alloc] initWithString:#""];
}
#end
#implementation NSString (NSStringCategory)
-(BOOL)matchedByPattern:(NSString *)pattern options:(int)options
{
NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options|REG_NOSUB];
return [re matchesString:self];
}
-(BOOL)matchedByPattern:(NSString *)pattern
{ return [self matchedByPattern:pattern options:0]; }
-(NSString *)substringMatchedByPattern:(NSString *)pattern options:(int)options
{
NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options];
return [re matchedSubstringOfString:self];
}
-(NSString *)substringMatchedByPattern:(NSString *)pattern
{ return [self substringMatchedByPattern:pattern options:0]; }
-(NSArray *)substringsCapturedByPattern:(NSString *)pattern options:(int)options
{
NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options];
return [re capturedSubstringsOfString:self];
}
-(NSArray *)substringsCapturedByPattern:(NSString *)pattern
{ return [self substringsCapturedByPattern:pattern options:0]; }
-(NSString *)escapedPattern
{
int len=[self length];
NSMutableString *escaped=[NSMutableString stringWithCapacity:len];
for(int i=0;i<len;i++)
{
unichar c=[self characterAtIndex:i];
if(c=='^'||c=='.'||c=='['||c=='$'||c=='('||c==')'
||c=='|'||c=='*'||c=='+'||c=='?'||c=='{'||c=='\\') [escaped appendFormat:#"\\%C",c];
else [escaped appendFormat:#"%C",c];
}
return [NSString stringWithString:escaped];
}
#end
I use the codes below to get the string between "" and ""
NSStringCategory *a=[[NSStringCategory alloc] initWithPattern:#"<title([\s\S]*)</title>" options:0];//
Unfortunately [a matchedSubstringOfString:response]] always returns nil
I do not if the regular expression is wrong or any other reason.
Welcome any comment
Thanks
interdev
(Preliminary warning: you can't parse HTML correctly with Regex.)
You are using regex.h, which provides POSIX regular expression (ERE in your case). They do not support all of the PCRE syntax such as \s and \S (and [\s\S] is useless anyway — it matches anything).
Probably you should use
initWithPattern:#"<title[^>]*>([^<]*)</title>" options:REG_ICASE
<title[^>]*>\([^<]*\)</title> should do the trick.
For this specific case, I might try instantiating a WebDocumentRepresentation object from the /System/Library/Frameworks/WebKit framework.
You could set the data source for the WebDocumentRepresentation object to the HTML page you were interested in, and then use the object's title method to return the title.
Here's the Mac OSX Reference Library document on the object.

How to implement initWithObjects?

How can I create a class with the initializer initWithObjects?
Or does it just make more sense to inherit from NSArray and work around it that way?
initWithObjects: is implemented using a C variable argument list. Here's an example implementation:
- (void)setContentByAppendingStrings:(NSString *)firstArg, ...
{
NSMutableString *newContentString = [NSMutableString string];
va_list args;
va_start(args, firstArg);
for (NSString *arg = firstArg; arg != nil; arg = va_arg(args, NSString*))
{
[newContentString appendString:arg];
}
va_end(args);
[contents autorelease];
contents = [newContentString retain];
}
See this page for more info.
#interface foo : NSObject {
NSArray* objects;
}
-(id)initWithObjects:(NSArray*)array;
#end
#implementation foo
-(id)initWithObjects:(NSArray*)array{
if(self = [super init]){
objects = array;
}
return self;
}
#end