问题描述
我正在尝试使我的应用程序更具动态性.我试图在一个类中声明一个函数,该类应该被我拥有的其他一些类实例化并使用.
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.
此刻我正在这样做.
// 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:该代码未复制粘贴,因此可能存在语法错误.
BTW: The code is not copy-pasted, so there may be syntax errors.
推荐答案
为FunBib创建实例
create instance for FunBib
-(void)viewDidLoad {[super viewDidLoad];self.funkBib = [[[FunkBib alloc] init];
somelabel.text = [self.funkBib formateraTillEEEEdMMMM:[someArray objectAtIndex:somewhere]];}
-(void)viewDidLoad{ [super viewDidLoad]; self.funkBib=[[FunkBib alloc] init];
somelabel.text = [self.funkBib formateraTillEEEEdMMMM:[someArray objectAtIndex:somewhere]];}
这篇关于在其他类别中达到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!