SEL作为参数的问题

兔子-顾问 2015-07-15 11:43:33
首先感谢打开关注我的问题
我正在学习ios开发。
我写了一个例子,需要执行NSMutabArray的排序功能。
我定义了两个类。
AddressCard//纪录卡片名,地址
AddressBook//纪录地址簿书名,NSMutabArray类型的对象book

我调用
[self.book sortUsingSelector:@selector(compareNames:)];

我在AddressCard中有一个方法如下
-(NSComparisonResult) compareNames:(id)element
{
return [((AddressCard*)element).name compare:self.name];
}

于是,就实现了排序。这里还没有问题,呵呵。抱歉,引言长了一点。

问题来了
1.似乎我没有办法在任何地方,查到这个SEL参数,应该写怎样的一个方法,也就是我怎么通过一个SEL参数,知道去写一个返回NSComparison结果,参数是一个类型为id的变量的?如果靠猜,我也许猜测是
-(NSComparisonResult) compareNames:(id)element another:(id)element2
这个格式,怎么可以看出来呢?因为并非所有的方法调用都是系统的,如果说系统的可以在网上查资料有迹可循,那如果我有一天用别人的方法,参数是SEL,我如何知道如何写这个方法呢?

2.我自己也尝试的想使用写一个SEL做参数的方法,不过总是无法实现好。网上看了一些,不过似乎没有很吻合的例子。如果有人了解,请不吝赐教。谢谢!


下面是我自己写的一个测试例子,当然,执行总提示参数错误,能告知如何错误的更好啦。

@interface SelectorDemo : NSObject
-(void)InvokeSel:(SEL) select_test_method;
@end

@implementation SelectorDemo
-(void) InvokeSel:(SEL)select_test_method
{
if(select_test_method)
{
@try {
[self performSelector:select_test_method withObject:nil];
}
@catch (NSException *exception) {
printf("[Name]\n%s\n[Reson]\n%s\n\n",[exception.name UTF8String],[exception.reason UTF8String]);
}
}
}
...全文
204 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2015-07-16
  • 打赏
  • 举报
回复
调用代码 main.m
#import <Foundation/Foundation.h>
#import "TRInvoker.h"
#import "TRSelTester.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        TRInvoker* invoker = [[TRInvoker alloc]init];
        TRSelTester* tester = [[TRSelTester alloc]init];
        tester.Invoker = invoker;
        
        //测试1 - NSInvalidArgumentException
        [tester InvokeSel:@selector(DisplaySentence)];
        
        //测试2 - 成员方法调用
        [invoker Invoke:tester];
    }
    return 0;
}
兔子-顾问 2015-07-16
  • 打赏
  • 举报
回复
已经解决问题了。 现在贴出完整的测试代码,比较新手有用,也只是为了让帖子完整 TRSelTester.h

#import <Foundation/Foundation.h>

@interface TRSelTester : NSObject
@property (nonatomic,strong) id Invoker;
-(void)InvokeSel:(SEL) select_test_method;
@end
TRSelTester.m

#import "TRSelTester.h"

@interface TRSelTester ()

@end

@implementation TRSelTester

-(void)InvokeSel:(SEL) select_test_method
{
    if(select_test_method)
    {
        @try {
            [self.Invoker performSelector:select_test_method withObject:nil];
        }
        @catch (NSException *exception) {
            printf("[Name]\n%s\n[Reson]\n%s\n\n",[exception.name UTF8String],[exception.reason UTF8String]);
        }
    }
}
@end
TRInvoker.h
#import <Foundation/Foundation.h>
#import "TRSelTester.h"

@interface TRInvoker : NSObject

-(void)DisplaySentence;
-(void)Invoke:(TRSelTester*) tester;

@end
TRInvoker.m
#import "TRInvoker.h"

@implementation TRInvoker
-(void)DisplaySentence
{
    printf("display a sentence by manual.\n");
}

-(void)Invoke:(TRSelTester*) tester
{
    [tester InvokeSel:@selector(DisplaySentence)];
}
@end
Bannings 2015-07-15
  • 打赏
  • 举报
回复
引用 2 楼 wuyazhe 的回复:
哈,终于有人回复了。 多谢多谢。 刚看书学到这里。 这样似乎明白了,意思SEL无法自我描述需要一个怎样签名的方法,需要另付文档说明。对吧。 还是2个问题 1.系统用到的SEL为参数的,似乎我在帮助中没有看到明确的说明,只有一段文字,例如: NSMutableArray的sortUsingSelector方法。 定义如下: - (void)sortUsingSelector:(SEL)comparator 对comparator的描述只有返回值 // A selector that specifies the comparison method to use to compare elements in the array. The comparator message is sent to each object in the array and has as its single argument another object in the array. The comparator method should return NSOrderedAscending if the array is smaller than the argument, NSOrderedDescending if the array is larger than the argument, and NSOrderedSame if they are equal. // 从这个描述,我如何能知道写出下面这样的方法呢?
-(NSComparisonResult) compareNames:(id)element
{
    return [((AddressCard*)element).name compare:self.name];
}
2.我这里是做练习,这个参数是SEL的例子,没做出来,比较不甘心,如果可以,能给一个简单例子,可以包含interface,implement和调用的么?有点小贪心啊,不过确实很期待。 喵。
你的 Demo 其实没问题,看你怎么调用吧。如果是这么调用的话就没问题:

SelectorDemo *demo = [SelectorDemo new];
[demo  InvokeSel:@selector(description)];
兔子-顾问 2015-07-15
  • 打赏
  • 举报
回复
哈,终于有人回复了。 多谢多谢。 刚看书学到这里。 这样似乎明白了,意思SEL无法自我描述需要一个怎样签名的方法,需要另付文档说明。对吧。 还是2个问题 1.系统用到的SEL为参数的,似乎我在帮助中没有看到明确的说明,只有一段文字,例如: NSMutableArray的sortUsingSelector方法。 定义如下: - (void)sortUsingSelector:(SEL)comparator 对comparator的描述只有返回值 // A selector that specifies the comparison method to use to compare elements in the array. The comparator message is sent to each object in the array and has as its single argument another object in the array. The comparator method should return NSOrderedAscending if the array is smaller than the argument, NSOrderedDescending if the array is larger than the argument, and NSOrderedSame if they are equal. // 从这个描述,我如何能知道写出下面这样的方法呢?
-(NSComparisonResult) compareNames:(id)element
{
    return [((AddressCard*)element).name compare:self.name];
}
2.我这里是做练习,这个参数是SEL的例子,没做出来,比较不甘心,如果可以,能给一个简单例子,可以包含interface,implement和调用的么?有点小贪心啊,不过确实很期待。 喵。
Bannings 2015-07-15
  • 打赏
  • 举报
回复
我们来讨论一下吧。 一般来说涉及到 SEL 的,几乎都是硬编码的,要么是你清楚这个方法的签名,要么是有统一的规则(如做 ORM 的时候,属性肯定会有 Getter 和 Setter 方法)。你想在运行时动态调用一个完全不知道会是什么的一个 SEL,就种方式是行不通的。 就算你用 NSMethodSignature 获取到了一个方法的参数和返回值,你打算如何处理呢?比如这个 SEL 的第一个参数是接收一个 NSNumber,你总不能随意实例化一个或者就是传递一个 nil 进去吧?

29,049

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧