开发者

Reaching a function in a different class

I'm trying to get my app a little more dynamic. I'm trying to declare a function in a class that is supposed to开发者_运维技巧 get instantiated and used by a few other classes i have.

At the moment i'm doing this.

// FunkBib.h

#import <Foundation/Foundation.h>


@interface FunkBib : NSObject {        
}
-(NSString *)formateraTillEEEEdMMMM:(id)suprDatum;
@end

// FunkBib.m

-(NSString *)formateraTillEEEEdMMMM:(id) suprDatum{        

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyyMMdd";        

    NSDate *date = [dateFormatter dateFromString:suprDatum];        

    NSLocale *swedishLocale=[[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"] autorelease];
    dateFormatter.locale=swedishLocale;        

    dateFormatter.dateFormat=@"EEEE d MMMM";
    NSString * weekdayString= [dateFormatter stringFromDate:date];

    NSString *newWeekDayString = [weekdayString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[weekdayString substringToIndex:1] capitalizedString]];

    return newWeekDayString;        
}

Meanwhile in my other classes i do this.

//someclass.h

#import <UIKit/UIKit.h>    

@class FunkBib;

@interface someclass : UIViewController {    

FunkBib *funkBib;

}
@property(nonatomic,retain)     FunkBib                         *funkBib;    
@end

// someclass.m

#import "FunkBib.h"    
@implementation someclass.m    
@synthesize funkBib;

And at a later point in the code i'd like to use this function like this.

somelabel.text = [funkBib formateraTillEEEEdMMMM:[someArray objectAtIndex:somewhere]];

I have no direct idea why this is not working. Anyone have any general pointers about how i might solve this?

BTW: The code is not copy-pasted, so there may be syntax errors.


create instance for FunBib

-(void)viewDidLoad{ [super viewDidLoad]; self.funkBib=[[FunkBib alloc] init];
somelabel.text = [self.funkBib formateraTillEEEEdMMMM:[someArray objectAtIndex:somewhere]]; }


you can use the static method if all the variables inside the methods are local variables.

+(NSString *)formateraTillEEEEdMMMM:(id) suprDatum{



    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyyMMdd";


    NSDate *date = [dateFormatter dateFromString:suprDatum];


    NSLocale *swedishLocale=[[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"] autorelease];
    dateFormatter.locale=swedishLocale;


    dateFormatter.dateFormat=@"EEEE d MMMM";
    NSString * weekdayString= [dateFormatter stringFromDate:date];

    NSString *newWeekDayString = [weekdayString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[weekdayString substringToIndex:1] capitalizedString]];

    return newWeekDayString;

}

call it by your class name

[FunkBib formateraTillEEEEdMMMM:obj];


It seems like your trying to manipulate a NSString here. Why don't your try creating a category for NSString instead of creating this object. It would mean you wouldn't have to create this object every-time you want this operation on a string performed. Plus is alot more extensible for future projects.

See this link for creating categories: http://macdevelopertips.com/objective-c/objective-c-categories.html

If i were creating this category it would look something like

NSString+FormattedDate.h :

@interface NSString (FormattedDate)
-(NSString *) formateraTillEEEEdMMMM;
@end

NSString+FormattedDate.m :

@implementation NSString (FormattedDate)

-(NSString *)formateraTillEEEEdMMMM {

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyyMMdd";

    NSDate *date = [dateFormatter dateFromString:self];

    NSLocale *swedishLocale=[[[NSLocale alloc] initWithLocaleIdentifier:@"sv_SE"] autorelease];
    dateFormatter.locale=swedishLocale;


    dateFormatter.dateFormat=@"EEEE d MMMM";
    NSString * weekdayString= [dateFormatter stringFromDate:date];

    NSString *newWeekDayString = [weekdayString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[weekdayString substringToIndex:1] capitalizedString]];

    return newWeekDayString;
}

@end

Then could call:

NSString *formattedDate = [NSString formateraTillEEEEdMMMM:@"whatever"];

Much nicer :)


Wouldn't changing the - in front of the method name to a + make it a static method, which you could then call "by class" instead of "by instance"?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜