Bittoo Bittoo - 2 years ago 59
iOS Question

How to generate numeric Numbers?

when ever i call My method I need pass the String like

next time
to server. If i use below code it is generating different numbers.

`-(NSString *)generateTitleString
NSString *title=[NSString stringWithFormat:@"Video %d",count+1];
return title;

But It is showing every time video 1 only how to get the sequential number.

Answer Source

It looks like you are not increasing the variable count and that is why you get always "Video 1".

UPDATE: You can create a class where you can keep all your global variables (like "count"). In this example, I'm naming this class GlobalData. You also need to use User Defaults to permanently save the value of some of your variables.

Try the next.


#import <Foundation/Foundation.h>

@interface GlobalData : NSObject

+ (NSInteger) getCount;

+ (void) setCount:(NSInteger)newCount;



#import "GlobalData.h"


@implementation GlobalData

* It returns the number of the last video displayed.
* @param 
* @return the number of the last video displayed
+ (NSInteger) getCount
    NSInteger count;
    NSString* recoveredValue;

    // init variables
    count = 0;
    recoveredValue = [[NSUserDefaults standardUserDefaults] valueForKey:COUNT_USER_DEFAULT_KEY];

    // If the recovered value is not NIL, we convert it to an integer.
    if (recoveredValue != nil)
        count = [recoveredValue integerValue];

    return count;

* It sets the number of the last video displayed with the value of input "newCount".
* @param newCount    Number of the last video displayed.
* @return 
+ (void) setCount:(NSInteger)newCount
    // Save user info in NSUserDefaults.
    [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%li", (long)newCount] forKey:COUNT_USER_DEFAULT_KEY];

    // Writes NSUserDefaults on disk.
    [[NSUserDefaults standardUserDefaults] synchronize];


View Controller:

#import "GlobalData.h"


- (void) viewDidLoad
     [super viewDidLoad];

- (NSString *) generateTitleString
    NSString *title;
    NSInteger count;

    // Getting count and increasing it.
    count = [GlobalData getCount];
    count = count + 1;        

    // Creating title by using 'count'.
    title = [NSString stringWithFormat:@"Video %d", count];

    // Updating 'count'
    [GlobalData setCount:count];

    return title;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download