我想知道:编写一个没有冒号的选择器名称@selector(mySelector)或者冒号的区别是什么@selector(mySelector:)?
如:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWith...
target:self
action:@selector(addAction:)];
Run Code Online (Sandbox Code Playgroud)
没有冒号我找不到另一个例子,但我很确定我已经看过其中一些了.
这很好,我们都知道:
NSString *textoutput = @"Hello";
outLabel.text = textoutput;
Run Code Online (Sandbox Code Playgroud)
但是,如果要在该NSString语句中包含变量,如下所示:
NSString *textoutput =@"Hello" Variable;
Run Code Online (Sandbox Code Playgroud)
在C++中我知道什么时候我cout想要包含一个变量我所做的就像这样:
cout << "Hello" << variableName << endl;
Run Code Online (Sandbox Code Playgroud)
所以我试图用Objective-C来实现它,但我不知道如何实现.
我已经看过很多关于向NSWindow添加控件以使其能够进入全屏幕的帖子 - 无论是通过代码还是通过Interface Builder设置.我正在寻找的是当应用程序启动时,默认情况下它会全屏打开.
我试图在针对iPhone的Xcode 4.2中制作一个小应用程序.我想要的是UIButton在屏幕上设置动画,当你按下它时,你将它的alpha设置为0.我从UIView类中找到了一个能够处理用户交互的方法,并且来到这个代码:
[UIView animateWithDuration:3.0
delay:0
options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent
animations: ^{ CGRect myRect = enemy1.frame;
myRect.origin.y = 300;
myButton.frame = myRect;
}
completion:nil];
Run Code Online (Sandbox Code Playgroud)
现在,这可以正确地完成动画,但事实是它立即设置origin.y为300,这意味着我不能按下按钮"当它向下滑动".我只能在那origin.y300个位置按下它,即使它还没有动画到那里,我也可以按下它.
Objective C中有哪些内容? @"%@" @"%d" @"%i"
stringWithFormat:是一个类的方法NSString,并返回一个自动释放的字符串; initWithFormat:是一个实例方法,在ARC之前,程序员必须处理返回对象的内存管理.如果我们启用ARC,这两种方法有什么区别?
cocoa memory-management objective-c nsstring automatic-ref-counting
我创建了一个NSTimer:
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(createObject:)
userInfo:nil
repeats:YES];
Run Code Online (Sandbox Code Playgroud)
并createObject:定义如下:
- (void)createObject:(ccTime) dt{
int r = arc4random() % 4;
for (int i=0; i < r; i++) {
character[charIndex] = [CCSprite spriteWithFile:@"o.png"];
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是将一些变量发送到方法中.我重写了这个函数:
- (void)createObject:(ccTime) dt cID:(int)cID {
int r = arc4random() % 4;
for (int i=0; i < r; i++) {
character[cID] = [CCSprite spriteWithFile:@"o.png"];
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法将变量传递cID给定时器中的函数.是否有可能做到这一点?
我的iOS应用程序中有以下类(它类似于Java世界中的抽象类).
@implementation WSObject
static NSDictionary* _dictionary = nil;
+(NSDictionary*) dictionary {
if (_dictionary == nil) {
_dictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:[self localStorePath]];
}
return _dictionary;
}
...
@end
Run Code Online (Sandbox Code Playgroud)
然后,我有多个类,WSObject使用类方法实现上述dictionary.问题是,这些类中的每一个都应该有自己的类_dictionary,但它们都是从超类共享同一个对象.当然,我可以复制到所有子类,但这会破坏可重用性.除了这个getter之外,还有其他类方法WSObject可以改变字典.因此,每个子类中都应该有几个类方法.
我怎样才能以聪明的方式解决这个问题?如果我的描述不充分,请告诉我.
我正在尝试为NSMethodSignature协议中声明的方法获取签名 - 对象或至少是类型编码字符串.
询问Protocol对象本身是不可能的,因为a)它没有实现methodSignatureForSelector:,而b)(如下面Kevin所述)它已被弃用.
运行时函数protocol_getMethodDescription返回a struct objc_method_description,文档中的任何位置都没有描述.它位于公共标题中,但是 - <objc/runtime.h>:
struct objc_method_description {
SEL name;
char *types;
};
Run Code Online (Sandbox Code Playgroud)
假设那里的types字符串将是与其他地方使用的相同类型的签名编码字符串似乎是合理的,例如预期的字符串+[NSMethodSignature signatureWithObjCTypes:],实际上它看起来是正确的.
我无法追踪的是该字符串与类型编码过程之间的实际可验证连接.
我不知道它会是什么,但是,我是否有任何理由依赖此types字符串在同一运行时与其他对象/函数的交互有效?请注意,我不是自己编写字符串或期望它们具有给定的格式或值 - 我只想将它们从运行时/框架的一部分传递给另一部分,即从协议中检索编码字符串和)NSMethodSignature如果一个对象不可用,则使用它来生成一个对象,并且可能b)将它与运行时生成的对象NSInvocation(即,in -forwardInvocation:)进行比较.
我应该只是变换一个变量,还是用isKindOfClass:它来检查然后再进行投射?哪一个更好,效率更高?(好吧,效率不仅仅是一个问题.)我想string在下面成为一个NSString.
例:
NSString *string = (NSString *)result;
Run Code Online (Sandbox Code Playgroud)
要么:
if(![string isKindOfClass:[NSString class]]
{
//cast it
}
Run Code Online (Sandbox Code Playgroud) objective-c ×10
cocoa ×4
cocoa-touch ×3
nsstring ×3
ios ×2
casting ×1
colon ×1
inheritance ×1
nstimer ×1
nswindow ×1
osx-lion ×1
protocols ×1
selector ×1
string ×1
subclassing ×1
uibutton ×1