我正在使用选择器动态调用Objective-C中的函数.我的问题是,如果我传递一个不存在的函数名,我的@try/ @catchblock没有捕获异常.
在控制台中,我得到了一个未被捕获的NSInvalidArgumentException进一步下来,我得到了一个未被捕获NSException.
我试过在标准@try/ @catch块中捕获这两种类型的异常,但它们没有注册.如果我试图抓住NSInvalidArgumentException它,Xcode将不会让我编译,因为它不是一个公认的类型.
关于如何解决这个问题的任何想法?
@try {
SEL s = NSSelectorFromString(@"funName");
[self performSelector:s withObject: nil];
}
@catch (NSException/NSInvalidArgumentException exception) {
NSLog(@"ERROR: %@", [exception message/reason];
}
Run Code Online (Sandbox Code Playgroud) 如果用户在特定时间内没有触摸屏幕,我必须执行特定操作.我怎么能在Cocos2d中做到这一点?
一个非常基本的问题,当我有类似的东西:
TTStyledText * text = [TTStyledText textFromXHTML:message.message lineBreaks:YES URLs:NO];
text.width = self.frame.size.width - 60;
text.font = [UIFont fontWithName:@"ArialMT" size:17.0];
_main_title.text = text;
Run Code Online (Sandbox Code Playgroud)
当我分配text时_main_title.text,是否意味着_main_title.text保留text?
有人可以通过循环显示如何检索值NSMutableArray吗?我的代码基本上为数组添加整数,如下所示:
NSMutableArray *ptr = [[NSMutableArray alloc] init];
[ptr addObject:[NSNumber numberWithInt:1]];
[ptr addObject:[NSNumber numberWithInt:2]];
[ptr addObject:[NSNumber numberWithInt:3]];
// How to retrieve them as integers?
Run Code Online (Sandbox Code Playgroud)
我正在尝试从数组中检索每个数字并将它们总计为总值.
[measureValue stringValue] 给了我这个例外:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSCFString stringValue]:无法识别的选择器发送到实例0x4dde570'
[measureValue description] 工作得很好
但我想我应该stringValue在代码中使用,对吧?
这是代码:
NSNumber *measureValue = [NSString stringWithFormat:@"%i", [cellContent integerValue]];
//if imperial (inches)
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"Measure"] isEqualToString:@"Imperial"] ) {
//if height
if ([currentQuestion isEqualToString:@"Height"]) {
measureValue = [NSNumber numberWithDouble:(2.54 * [measureValue doubleValue])];
//elseif weight
} else {
measureValue = [NSNumber numberWithDouble:(0.45359237 * [measureValue doubleValue])];
}
//NSLog(@"%@", [measureValue stringValue]);
cellContent = [measureValue description];
Run Code Online (Sandbox Code Playgroud) 我在基类中有一个方法调用,如下所示:
#import <Foundation/Foundation.h>
@interface BaseController : NSObject
-(void)login;
@end
Run Code Online (Sandbox Code Playgroud)
#import "BaseController.h"
@implementation BaseController
-(void)performTask
{
return @"Base method loaded";
}
-(void)login
{
[self performTask];
}
-(id)init
{
if ((self = [super init]))
{
}
return self;
}
-(void)dealloc
{
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
#import <Foundation/Foundation.h>
#import "BaseController.h"
@interface DerivedController : BaseController
-(void)performTask;
@end
Run Code Online (Sandbox Code Playgroud)
#import "DerivedController.h"
@implementation DerivedController
-(void)performTask
{
NSLog(@"Inherited method loaded.");
}
-(id)init
{
if ((self = [super init])) { }
return self;
}
-(void)dealloc
{
[super dealloc];
} …Run Code Online (Sandbox Code Playgroud) 我正试图转变@"Fri, 26 Aug 2011 10:51:00 +02:00"为NSDate:
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
Run Code Online (Sandbox Code Playgroud)
我得到nil了结果; 我究竟做错了什么?
我有这段代码用于显示登录对话框/发布到用户墙:
_posting = YES;
// If we're not logged in, log in first...
if (![_session isConnected]) {
self.loginDialog = nil;
loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
// If we have a session and a name, post to the wall!
else if (_facebookName != nil) {
[self postToWall];
}
// Otherwise, we don't have a name yet, just wait for that to come through.
Run Code Online (Sandbox Code Playgroud)
如果我将此代码连接到按钮,它可以工作.但是,当我将它放在以下方法中时:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons …Run Code Online (Sandbox Code Playgroud) 我正在尝试UIWebView使用以下代码检查a中的当前URL :
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *currentURL = webView.request.URL.absoluteString;
NSLog(@"Current URL: %@", currentURL);
NSString *loginpage = @"file:///var/mobile/Applications/652BAC0C-DEAE-4695-9195-BE617B9D5106/School%20VLE%20Business.app/login.html";
if([currentURL isEqualToString:loginpage])
{
NSLog(@"Inputting user details...");
NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#usernamefield').val('%@');", username]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#passwordfield').val('%@');", password]];
result = [webView stringByEvaluatingJavaScriptFromString:@"$('#submitbutton').click();"];
NSLog(@"Logged in!");
}
}
Run Code Online (Sandbox Code Playgroud)
但是本地URL(652BAC0C-DEAE-4695-9195-BE617B9D5106对我来说)中的代码对于每个人来说都不一样,所以我怎么能做同样的检查却没有长代码呢?
我正在尝试学习NSTimer,使用Foundation和打印到控制台.任何人都可以告诉我我需要做些什么来让以下工作?它编译没有错误,但不激活我的startTimer方法 - 没有打印.
我的目标是让一个方法调用另一个方法来运行一些语句,然后在设定的时间后停止.
#import <Foundation/Foundation.h>
@interface MyTime : NSObject {
NSTimer *timer;
}
- (void)startTimer;
@end
@implementation MyTime
- (void)dealloc {
[timer invalidate];
[super dealloc];
}
- (void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(runTimer:) userInfo:nil repeats:YES];
}
- (void)runTimer:(NSTimer *)aTimer {
NSLog(@"timer fired");
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MyTime *timerTest = [[MyTime alloc] init];
[timerTest startTimer];
[timerTest release];
[pool release];
return 0;
}
Run Code Online (Sandbox Code Playgroud) objective-c ×10
ios ×4
cocoa ×3
cocoa-touch ×3
nsnumber ×2
exception ×1
facebook ×1
foundation ×1
inheritance ×1
loops ×1
macos ×1
methods ×1
nsexception ×1
nsstring ×1
nstimer ×1
parsing ×1
properties ×1
touch ×1
try-catch ×1
uialertview ×1
uiwebview ×1