以下代码给出了错误:
// constants.h
extern NSArray const *testArray;
Run Code Online (Sandbox Code Playgroud)
// constants.m
NSArray const *testArray = [NSArray arrayWithObjects: @"foo", @"bar", nil];
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
initializer element is not constant
或者,如果我拿走指针指针(*),我得到:
statically allocated instance of Objective-C class 'NSArray'
是否可以在文本中添加阴影UITextField
?
所以,我有这个定义:
typedef enum {
red = 1,
blue = 2,
white = 3
} car_colors;
Run Code Online (Sandbox Code Playgroud)
然后,我有一个car_colors类型的变量:car_colors myCar;
问题是,我收到了NSString中汽车的颜色.它必须是NSString,我不能改变它.如何从NSString转换为car_colors类型?
NSString *value = [[NSString alloc] initWithString:@"1"];
myCar = [value intValue]; // <-- doesn't work
Run Code Online (Sandbox Code Playgroud)
任何的想法?谢谢!
我想在我的应用中播放Youtube视频.所以,我写了下面的代码
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, videoURL, self.view.frame.size.width, self.view.frame.size.height];
[videoView loadHTMLString:html baseURL:nil];
Run Code Online (Sandbox Code Playgroud)
videoView是一个UIWebView.这工作得很好,直到以后,视图没有显示任何内容.只是一个空白的白色视图.我得到了这个日志:
*** WebKit discarding exception: <NSRangeException> *** -[__NSCFString substringToIndex:]: Range or index out of bounds
Run Code Online (Sandbox Code Playgroud) 好的.希望这将是我关于我在Objective-C中编写的下载管理器的最后一篇文章.除了暂停/恢复功能外,一切似乎都运行良好.我的问题是,当下载尝试从停止的地方继续下载时,它会将收到的数据附加到文件中,但它似乎仍在尝试下载整个文件.这会导致文件大于原始文件.这是我用于下载文件的代码.难道我做错了什么?
-(void)start:(unsigned int)fromByte {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:DEFAULT_TIMEOUT];
// Define the bytes we wish to download.
NSString *range = [NSString stringWithFormat:@"bytes=%i-", fromByte];
[request setValue:range forHTTPHeaderField:@"Range"];
// Data should immediately start downloading after the connection is created.
self.urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:TRUE];
if (!self.urlConnection) {
#warning Handle error.
}
}
Run Code Online (Sandbox Code Playgroud) 在我的程序中,我根据旋转移动东西,但我不是在旋转整个视图.我正在使用 :
static UIDeviceOrientation previousOrientation = UIDeviceOrientationPortrait;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) didRotate:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self doRotationStuff:orientation: previousOrientation];
previousOrientation = orientation;
}
Run Code Online (Sandbox Code Playgroud)
只要程序启动时,设备方向是纵向,但如果初始方向是横向或颠倒,则无效,因为[self doRotationStuff]相对于与前一个方向的差异进行更改.
有没有办法在发射时或在旋转设备之前检测方向?
我有一个令人难以置信的令人沮丧的问题似乎是一个错误,但我很难相信没有其他人遇到过这个问题.我的应用程序的根视图控制器是一个UITabBarController,其中每个选项卡都是UINavigationController.一切都很好.
现在我来到一个我想编辑堆栈的地方,所以我重新排列当前导航控制器的viewControllers然后执行:
[self.navigationController setViewControllers:newViewControllers animated:YES];
Run Code Online (Sandbox Code Playgroud)
堆栈被正确弹出/推送到顶视图控制器,但导航栏不会更新到当前视图控制器,并且似乎保持与popCon之前的viewController完全一样.如果我做:
[self.navigationController popToViewController:someViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)
一切都很完美.有没有人曾经遇到过这个?有解决方法吗?我做错了什么?
如何初始化头文件中的常量?
例如:
@interface MyClass : NSObject {
const int foo;
}
@implementation MyClass
-(id)init:{?????;}
Run Code Online (Sandbox Code Playgroud) 有没有办法询问编译器是否打开ARC,然后根据该值有条件地编译?例如,我有一个协议:
@protocol ProtocolA
@required
-(void)protocolMethodOne
@optional
-(void)protocolMethodTwo;
@end
Run Code Online (Sandbox Code Playgroud)
如果我使用ARC,我想protocolMethodA
在使用ARC时可选,并且在不使用ARC时需要.这是因为使用此方法的主要原因之一是取消分配对象实例.
话虽如此,这就是我想要发生的事情:
@protocol ProtocolA
#ifdef SOME_ARC_VARIABLE
@optional
#else
@required
#endif
-(void)protocolMethodOne
@optional
-(void)protocolMethodTwo;
@end
Run Code Online (Sandbox Code Playgroud) iphone objective-c conditional-compilation automatic-ref-counting
objective-c ×8
iphone ×7
constants ×2
arrays ×1
cocoa ×1
cocoa-touch ×1
download ×1
enums ×1
header ×1
html ×1
http-headers ×1
ios ×1
ipad ×1
shadow ×1
uikit ×1
uitextfield ×1
uiwebview ×1