【问题标题】:Method syntax in Object C [closed]对象 C 中的方法语法
【发布时间】:2012-08-15 05:07:03
【问题描述】:

谁能为我解释下面的方法声明语法?我无法理解“(void)”旁边的“connection:(NSURLConnection *)connection”部分

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

谢谢。

【问题讨论】:

    标签: objective-c syntax methods


    【解决方案1】:
     -                             // method type. - is instance method, + is class method
     (void)                        // return type
     connection:                   // method name
     (NSURLConnection *)connection // first argument and its type
     didReceiveResponse:           // method name continues
     (NSURLResponse *)response     // second argument and its type
    

    但是您可能应该找一本书并实际学习 Obj-C。如果你不理解语法,你还有很长的路要走。

    【讨论】:

    • 你应该把 - 放在单独的一行,“这是一个实例方法。”
    【解决方案2】:

    这样想:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    
    1. void 是返回类型;

    2. 该方法采用connection 类型的第一个参数NSURLConnection*

    3. 该方法采用第二个参数,称为response,类型为NSURLResponse*

    编写方法签名的方式不同。如果您认为它会有所帮助:

    - (void)connection:didReceiveResponse:
    

    Obj-C(与 C 或 C++ 相比)的特点是参数在签名中混杂在一起。这样做的好处是您可以轻松地为方法调用中的每个参数命名:

    [connection:currentConnection didReceiveResponse:lastResponse];
    

    【讨论】:

      【解决方案3】:

      它在 Cocoa 中的委托回调中传递原始对象的标准,在这种情况下是已收到响应的 NSURLConnection。

      如果您一般不理解方法语法,那么可能阅读目标 c,并在此处查看其他答案

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 2011-04-25
        • 2016-04-20
        • 2013-09-06
        • 2015-12-20
        • 2015-12-02
        • 1970-01-01
        • 2021-03-25
        • 2023-04-09
        相关资源
        最近更新 更多