Giter Site home page Giter Site logo

swift-error's Introduction

swift-error

swift遇到错误收集

[Function错误]定义两个同名的方法

Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector

####问题描述

在定义一个同样的方法名时出现了如下错误

Problem1

class ViewController: UIViewController {

    func perform(operation : Double -> Double)
    {
        
    }
    
    func perform(operation : NSData)
    {
        
    }
}

####问题分析

首先,swift是支持重载(overloading)的,上面方法名不一样但是参数类型不一样,按照重载的理解,上面的代码不应该有问题。但是问题就是有了。下面看看解答。

解答:虽然swift是支持重载,但是OC不支持,上面的ViewController是继承的UIViewController,就是说OC和Swift都可以使用这个类。编译器会将此类用OC的编译方式检测,所以会报错。

扩展: 但是,如果上面代码改为

class ViewController: UIViewController {

    func perform(operation : Double -> Double)
    {
        
    }
    
    func perform(operation some : NSData)
    {
        
    }
}

就不会报错,不过,如果写成这样又会报错,

class ViewController: UIViewController {

    func perform(#operation : Double -> Double)
    {
        
    }
    
    func perform(operation some : NSData)
    {
        
    }
}

这样又会有问题。这里就是要说一下Swift转OC的方式。

如果一个swift类在OC中使用的时候,编译器会将swift类转换成OC的类进行处理。

func perform(operation : Double -> Double) {}

这个方法会被转换为

-(void)perform:(Double (^)(Double))block {}

这个方法

func perform(operation: NSData) {}

会被转换为

-(void)perform:(NSData *)operation {}

这两个方法在OC里面是不行的,所以会报错。

不过如果我们把方法改为下面这个

func perform(operation some : NSData) {}

或者

func perform(#operation : Double -> Double) {}

会相应的转换为

-(void)performWithOperation:(Double (^)(Double))block {}
-(void)performWithOperation:(NSData *)operation {}

这样就能理解了。

swift-error's People

Contributors

yyllove123 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.