开发者

How to make a global integer that works with substringFromIndex?

开发者 https://www.devze.com 2023-02-01 08:38 出处:网络
Merry Christmas!!! I\'ve ran into a little issue and I need help. I have NSString that is like so: NSString *newStr = [someString substringFromIndex:(someNum)];

Merry Christmas!!! I've ran into a little issue and I need help. I have NSString that is like so:

NSString *newStr = [someString substringFromIndex:(someNum)];

I'm trying to define (someNum) globally. I believe it needs a NSUInteger to work probably. The reason I need it defined globally is because I'm using some "if statements" to define someNum and someNum will return undeclared if not global. Is it possible to use a objectForKey with this NSString? I'm just stuck on what to do. I tried to define someNum with:

#define someNum 5

but I will have multiple instances of someN开发者_StackOverflow社区um and it won't work. Like so:

if (someName = @"abc"){
#define someNum 5
}
if (someName = @"abcd"){
#define someNum 6
}
NSString *newStr = [someString substringFromIndex:(someNum)];

I also tried:

if (someName = @"abc"){
NSUInteger someNum = 5;
}
if (someName = @"abcd"){
NSUInteger someNum = 6;
}
NSString *newStr = [someString substringFromIndex:(someNum)];


Your if statements are completely incorrect, you are performing pointer comparison on the objects, you need to use NSString's isEqualToString: method:

extern NSUInteger someNum;

if([someName isEqualToString:@"abc"]) {
   someNum = 5;
} else if([someName isEqualToString:@"abcd"]) {
   someNum = 6;
}

NSString *newStr = [someString substringFromIndex:someNum];
0

精彩评论

暂无评论...
验证码 换一张
取 消