我有两个字符串:
NSString * className = "MyClass";
NSString * methodName = "doSomething";
Run Code Online (Sandbox Code Playgroud)
MyClass类定义和静态方法doSomething也存在.
如何从两个字符串动态触发[MyClass doSomething]?
有什么用 -
func perform(_ aSelector: Selector!) -> unmanaged<AnyObject>!
在iOS?
我原本以为它是添加一个选择器方法,但在看了它之后我想它可能是"调用一个类的方法".选择器用于目标/动作范例,其中我踢掉了一些东西,当事件触发或结束时,它想要触发某种动作方法.在这个例子中,我传递一个选择器函数的参数,然后在这个"执行"函数的末尾,我返回一个任何类型的非托管对象?这甚至有意义吗?
谢谢!
假设我有一个可以分配给几种不同方法的选择器 - 每个方法都有不同的返回值.
有没有办法在调用"performSelector"之前检查选择器持有的方法的返回值是什么?
我有两个类,Class1和Class2,第二个继承自第一个类.我需要覆盖Class1的-update方法来实现我的目标.继承方法中-update方法的更改是在代码中间执行的,所以我不能使用[super update].这就是为什么我需要将原始方法从父类复制粘贴到继承类.此方法使用父方法的私有方法,因此当我尝试覆盖时,我收到有关缺少私有方法的警告,因为Class2仅导入Class1.h.澄清一下,这是代码:
Class1.h:
@interface Class1 : NSObject
-(void) update;
@end
Run Code Online (Sandbox Code Playgroud)
Class1.m:
@interface Class1 (Private)
-(void) private1;
-(void) private2;
@end
@implementation Class1
-(void) update
{
[self private1];
[self private2];
}
-(void) private1
{
// some code
}
-(void) private2
{
// another code
}
@end
Run Code Online (Sandbox Code Playgroud)
Class2.h:
@interface Class2 : Class1
-(void) update;
@end
Run Code Online (Sandbox Code Playgroud)
Class2.m:
@implementation Class2
-(void) update
{
[self private1]; // warning here
// do my own stuff between private methods, that is the reason of inheritance
[self private2]; // …Run Code Online (Sandbox Code Playgroud) 我想创建一个方法并使用字符串值"redColor"为标签设置UIColor.tableColorName是NSString"redColor",我尝试应用选择器来创建UIColor并将其应用于我的textLabel.谢谢
-(void) changeLabelColor
{
SEL labelColor = NSSelectorFromString([NSString stringWithFormat:[@"%@", tableColorName]]);
UIColor *color = [[UIColor class] performSelector:labelColor];
self.textLabel.textColor = color;
}
Run Code Online (Sandbox Code Playgroud) 如何在不同线程的后台执行某些操作,如果它在主线程上执行则会阻止我的应用程序的UI.任何人都知道该怎么做?
即使它在后台打印NSLog也没关系.即使用户按下HOME按钮,我也希望运行以下内容.在我的viewController中我这样做了:
- (IBAction)btnStartClicked:(UIButton *)sender {
[NSThread detachNewThreadSelector:@selector(StartBGTask) toTarget:self withObject:nil];
}
-(void)StartBGTask{
[[[UIApplication sharedApplication] delegate] performSelector:@selector(startThread)];
}
Run Code Online (Sandbox Code Playgroud)
并在appDelegate.mi中有这个方法
-(void) startThread {
@autoreleasepool {
for (int i = 0; i < 100; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"current progress %d", i);
});
[NSThread sleepForTimeInterval:1];
}
}
}
Run Code Online (Sandbox Code Playgroud)
它以1秒的间隔打印1到100之间的整数.
大家好,我想使用iOS8的新APN来处理通知操作.在这个方法中:
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]) {
[self handleAcceptActionWithNotification:notification];
}
else if([identifier isEqualToString:@"MAYBE_IDENTIFIER"]) {
[self handleMaybeActionWithNotification:notification];
}
else if ([identifier isEqualToString:@"REJECT_IDENTIFIER"]) {
[self handleRejectActionWithNotification:notification];
}
else if....blah blah blah..
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可能不得不在将来用NSString编写太多if-else语句,并且我知道某种方法可以避免使用过多的if-else语句,例如使用switch,但它不适用于字符串或NSString情况.
是避免在此字符串或NSString情况下编写过多if-else语句的解决方案吗?提前致谢.
我接下来写.所有代码都用ARC写的
@interface MPEvent : UIImageView
@property (nonatomic, unsafe_unretained) SEL action;
@property (nonatomic, strong) id target;
@end
Run Code Online (Sandbox Code Playgroud)
选择器是以正确方式声明的吗?
在实现中,我接下来使用我的属性:
- (void)sendActionToTargetFromView:(id)view {
[target performSelector:action withObject:view];
}
Run Code Online (Sandbox Code Playgroud)
但编译器向我显示警告
警告:语义问题:PerformSelector可能导致泄漏,因为其选择器未知
如何修复此警告?
我有一个Objective-c的情况,特别是对于iOS应用程序开发,一个特定的方法将执行并返回一个数字,从01 - 20.此外,我有20种不同的方法,名称为m01,m02,m03等.
如何编写我的代码,以便我的第一个方法调用另一个方法,该方法对应于第一个方法返回的数字?
像这样的东西:
[self m[NSStringWithFormat=@"%i", myOutputFromMethod1];
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我让这个工作吗?
我正在尝试从nsarray执行performSelector选择方法,但我收到此错误:
performSelector可能导致泄漏,因为它的选择器未知
这是我的代码:
-(void)exceudeMethods
{
NSArray *methods = @[@"methodOne",@"methodTwo"];
for (int i =0 ; i < methods.count; i++)
{
[self performSelector:NSSelectorFromString([NSString stringWithFormat:@"%@:",methods[i]])];
}
}
Run Code Online (Sandbox Code Playgroud)
如果我替换你们中的任何人知道如何解决这个问题,那么我可以执行nsarray中的所有方法?
ios ×9
objective-c ×7
iphone ×2
nsstring ×2
selector ×2
if-statement ×1
ipad ×1
methods ×1
nsthread ×1
return-value ×1
swift ×1
uicolor ×1
uicontrol ×1
uiimageview ×1