I have this method and I need to concatenating parameters so the result should be. Any idea how to do it?
getTyreLabels?width=m125.00&aspect=25&rim=13.00&season=SU&time=269742091
- (NSURL *)getTyreLabels:(NSString*)width :(NSInteger*)aspect : (NSInteger*)rim : (NSString*)season : (NSString*)pattern : (NSInteger*)time;
{
return [[self getBaseUrl] URLByAppendingPathComponent:#"getTyreLabels"];
}
I'm guessing that the string you put at the beginning is the desired output.
In which case your method should be something like...
- (NSString *)parameterStringWithWidth:(NSString *)width
aspect:(NSInteger)aspect
rim:(NSInteger)rim
season:(NSString *)season
pattern:(NSString *)pattern
time:(NSInteger)time
{
return [NSString stringWithFormat:#"getTyreLabels?width=m%#&aspect=%ld&rim=%ld&season=%#&time=%ld", width, (long)aspect, (long)rim, season, (long)time];
}
That will return the string. Not the URL but you should be able to get the point from this.
Note the way the method name is constructed. It makes it MUCH easier to call it from somewhere else as you can see what each parameter is relating to.
NSString *theString = [self parameterStringWithWidth:#"125.00" aspect:25 rim:13 season:#"SU" pattern:#"" time:269742091];
This will result in theString being the value you put in your question.
Might be you want something like this -
- (NSURL *)getTyreLabelsWithWidth:(NSString*)width
andAspect:(NSInteger)aspect
andRim:(NSInteger)rim
andSeason:(NSString *)season
andPattern:(NSString*)pattern
andTime:(NSInteger)time
{
NSString *stringToAppend = [NSString stringWithFormat#"getTyreLabels?width=%#&aspect=%d&rim=%d&season=%d&time=%d", width, aspect, rim, season, time];
return [[self getBaseUrl] URLByAppendingPathComponent: stringToAppend];
}
Related
I know there's a lot of questions like this around, but I think my situation's a tad different.
int i = 0;
while (_data[#"VerticalState%i", i] != nil) {
// do things
i++;
}
For example, one 'level' that has 3 VerticalState properties will be implemented as such: VerticalState0, VerticalState1, VerticalState2.
I want to read in those values using that while loop condition above, and it should stop when i = 3. How can I make the idea of that code above work (with some other configuration obviously). FYI, _data is an NSDictionary* instance variable, already loaded with the plist information.
You appear to want to create a dictionary key from a string format. You need to use NSString stringWithFormat:.
while (_data[[NSString stringWithFormat:#"VerticalState%i", i]] != nil) {
Though it would be better to write the loop like this:
int i = 0;
while (1) {
NSString *key = [NSString stringWithFormat:#"VerticalState%i", i];
id value = _dict[key];
if (value) {
// do things
i++;
} else {
break;
}
}
-(void) print {
NSLog(#"%i/%i", numberator, denominator);
}
Fraction *myFraction = [Fraction new];
[myFraction setNumberator:1];
[myFraction setDenominator:3];
Now I have other methods made like dividing both numerator and denominator etc. But i want to add in my [myFraction] in the NSLog with everything else rather than on a new line
NSLog(#"The value of my fraction is: %i/%i which equals to: %.3g * 100 = %.4g%%", [myFraction numberator], [myFraction denominator], [myFraction convertToNum], [myFraction multiplyBy100]);
is there anyway i can add in the [myFractions print] where the two %i %i is, so i don't have to type in [myFraction numerator/denominator]
eg.
NSLog(#"%#", [myFractions Print];
result: 1/3
You can usedescription method. You can override it in your class to return string representation of your class.
In your case you can use the following code:
-(NSString*) description {
return [NSString stringWithFormat:#"%i/%i", numerator, denominator];
}
to print the object you can then use it like this
NSLog("Fraction = %#", yourFractionObj);
See Describing objects.
Instead of your print method, override the description method on your Fraction class like this:
- (NSString *)description {
return [NSString stringWithFormat:#"%i/%i", numberator, denominator];
}
The description method is automatically called when you print an object using NSLog, so you can now write logs like this:
NSLog(#"The value of my fraction is: %# which equals to: %.3g * 100 = %.4g%%", myFraction, [myFraction convertToNum], [myFraction multiplyBy100]);
If you need the print method for another reason you can now write it like this:
- (void) print {
NSLog(#"%#", self);
}
You can use func or FUNCTION. It will print the method name with class name. For Example
NSLog(#"%s",__func__);
What if I have a method that returns a CGFloat and that method could not find an expected number, I would like to return something like NSNotFound, but that is an NSInteger.
Whats the best practice for this ?
You could use not a number (NaN).
See nan(), nanf() and isnan().
However for these issues, where there is no clearly defined non-value (it's worse with integers), then I prefer to use the following method semantics:
- (BOOL)parseString:(NSString *)string
toFloat:(CGFloat *)value
{
// parse string here
if (parsed_string_ok) {
if (value)
*value = parsedValue;
return YES;
}
return NO;
}
A pretty clean way is to wrap it into an NSNumber:
- (NSNumber *)aFloatValueProbably
{
CGFloat value = 0.0;
if (... value could be found ...) {
return #(value);
}
return nil;
}
Then you can check if the function returned nil for your non-existing value.
I want to switch NSString in XmlParser because if there are 15 or more web-service then every time the loop check for correct element in IF..ELSE.That I don't want to make processor busy..
I have searched a lot and found using enum I can switch NSString but no luck ..
I have tried each possibilities,but some where i am making mistake.
Please help to solve this big problem for me.
Here I have declare my enum:
Here in "elementName" I am getting Exact value as declared in enum:
But instead of 1, I am getting wrong value Like 202896536:
You cant do it by creating enum. You must need to compare the string.
if([elementName isEqualToString:#"UserLoginComplexType"])
//Do something...
You can not cast a string to ENUM value, you will need to parse it, ENUM values are integers not strings.
You will have to use an if statement.
You could use a helper method:
WebServiceList.h
typedef NS_ENUM(NSUInteger, WebServiceList) {
WebServiceListNone = 0,
UserLoginComplexType = 1,
RegisterUserResult = 2,
RecoverPasswordResult = 3,
....
};
FOUNDATION_EXTERN WebServiceList WebServiceListForString(NSString *string);
WebServiceList.m
WebServiceList WebServiceListForString(NSString *string) {
WebServiceList list = WebServiceListNone;
if (![type isKindOfClass:[NSString class]]) {
return CallRecordTypeNone;
}
else if ([string isEqualToString:#"UserLoginComplexType"] {
list = UserLoginComplexType;
}
else if ([string isEqualToString:#"UserLoginComplexType"]) {
list = UserLoginComplexType;
}
else .....
return list;
}
As seen in your commented codes, you're parsing a XML and saving in a NSMutableArray named arrProductList in App Delegate.
After finishing the parsing of XML, the variable should contain the data in array. You should look into the variable & fetch the corresponding value. Since you didn't post any further details about parsing / XML structure, I'm unable to write some codes related to result fetching.
For easy readability and to avoid lots of if-else statements, I like to do mine as a dictionary:
(also makes it easy to update in the future when you add more to your enum)
NSString* elementName = ...;
// Default value
WebServiceList value = UserLoginComplexType;
NSDictionary* stringToEnum = #{#"UserLoginComplexType":#(UserLoginComplexType),
#"RegisterUserResult":#(RegisterUserResult),
#"RecoverPasswordResult":#(RecoverPasswordResult)};
NSNumber* enumValue = stringToEnum[elementName];
if(enumValue != nil)
value = (WebServiceList)enumValue.integerValue;
iOS 5.0 SDK
I have a method that took a parameter as a 'type' that I defined. Lets call it 'Places'. This type was defined as the following:
typedef enum {
kBar = 0,
kRestaurant = 1,
kCafe = 2
} Places
My method would take a parameter of Places.
Based on the Place type passed in, I would append the type to the url:
ex: http://www.domain.com/place=1
However, the url parameter cannot be a number it has to be a string.
ex: http://www.domain.com/place=restaurant
I know enums cannot be strings so I am trying to figure out the right approach for this. Do I have a plist and then read the plist into a dictionary? Is there another way?
I would do something like:
typedef enum {
PlaceTypeBar = 0,
PlaceTypeRestaurant = 1,
PlaceTypeCafe = 2
} PlaceType
#interface PlaceTypeHelper : NSObject
+ (NSString *) stringForPlace:(PlaceType)place;
#end
#implementation
+ (NSString *) stringForPlace:(PlaceType)place {
NSArray *places = [NSArray arrayWithobjects:#"Bar", #"Restaurant", #"Cafe", nil];
return [places objectForKey:(NSInteger)place];
}
#end
Headups, I've no tested the code yet.
There's a lot of different approaches you could take. Here's what I might do myself.
Assuming there's a finite and known amount of values, you can do a simple function which returns the string for the given type :
(NSString*) StringForPlaceType(PlaceType thePlace) {
switch(thePlace) {
case kBar:
return #"Bar";
case kRestaurant:
return #"Restaurant";
case kCafe:
return #"Cafe";
default:
// ...
}
}
No need for an object or class unless you want for flexibility such as dynamic values and such.