斯威夫特如何“通过价值”一个对象 [英] Swift how to "pass by value" of a object

查看:57
本文介绍了斯威夫特如何“通过价值”一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift的新手。我创建了一个类(例如):

I am quite new in Swift. And I create a class(for example):

class Fraction{
    var a: Int
    init(a:Int){
        self.a = a
    } 
    func toString() -> String{
        return "\(self.a)"
    }
}

我还构建了一个其他类函数:

and I also build a in other class function:

class func A_plusplus(f:Fraction){
    f.a++
}

然后在执行课中我写道:

Then in the executive class I write:

var object = Fraction(a:10)
print("before run func = " + object.toString())
XXXclass.A_plusplus(object)
print("after ran func =" + object.toString() )

因此,在运行func = 10之前,控制台输出

So the console output is


;在运行func = 11

问题是如何发送对象的副本以保持其值等于10

The question is how can I just send a copy of the "object" to keep its value which equal to 10

如果函数总是按引用传递,为什么我们仍然需要关键字:inout

And if functions are always pass-by-reference, why we still need the keyword: "inout"

A_plusplus(& object)之间的区别是什么// [如果我将参数设为inout参数] A_plusplus(对象)


一般情况下,我不喜欢我不想使用 struct 。虽然这会完全解决我的
问题,但我很少按价值传递。所以我不希望程序的
复制过程会降低用户手机的速度:(

Universally, I don't want to use struct. Although this will solve my problem exactly, I do pass-by-value rarely.So I don't want program's copying processes slow my user's phone down :(

似乎符合 NSCopying 协议是个不错的选择。但是
我不知道如何实现这个功能:
func copyWithZone(zone:
NSZone) - > AnyObject?
正确

And It seems conforming the NSCopying protocol is a good option.But I don't know how to implement the function: func copyWithZone(zone: NSZone)-> AnyObject? correctly


推荐答案

如果您的类是NSObject的子类,最好使用 NSCopying

If your class is subclass of NSObject,better to use NSCopying

class Fraction:NSObject,NSCopying{
var a:Int
var b:NSString?
required init(a:Int){
    self.a = a
}
func toString() -> String{
    return "\(self.a)"
}
func copyWithZone(zone: NSZone) -> AnyObject {
    let theCopy=self.dynamicType.init(a: self.a)
    theCopy.b = self.b?.copy() as? NSString
    return theCopy
}
}
class XXXclass{
class func A_plusplus(f:Fraction){
    f.a++
    f.b = "after"
}

}

var object = Fraction(a:10)
object.b = "before"
print("before run func = " + object.toString())
print(object.b!) //"Before"

XXXclass.A_plusplus(object.copy() as! Fraction)
print("after ran func =" + object.toString() )
print(object.b!)//"Before"

如果它只是一个普通的swift类,你必须创建一个复制方法

If it is just a common swift class,You have to create a copy method

class Fraction{
var a: Int
init(a:Int){
    self.a = a
}
func toString() -> String{
    return "\(self.a)"
}
func copy()->Fraction{
    return Fraction(a: self.a)
}
}
class XXXclass{
    class func A_plusplus(f:Fraction){
        f.a++
   }
}
var object = Fraction(a:10)
print("before run func = " + object.toString())
XXXclass.A_plusplus(object.copy())
print("after ran func =" + object.toString() )

为了说清楚,你必须知道有主要有两种类型的快速

To make it clear,you have to know that there are mainly two types in swift


  1. 参考类型。与类实例类似,函数类型

  2. 值类型,类似于struct和其他(非类实例或函数类型)

  1. Reference types. Like Class instance,function type
  2. Value types,Like struct and others(Not class instance or function type)

如果您传入参考类型,则传递参考的副本,它仍然指向原始对象。

If you pass in a Reference types,you pass in the copy of Reference,it still point to the original object.

如果传入复制类型,则会传入值的副本,因此它与原始值无关

If you pass in a Copy type,you pass in the copy of value,so it has nothing to do with the original value

让我们谈谈 inout ,如果你使用它,它会传入相同的对象或值。它会影响值类型

Let us talk about inout,if you use it,it pass in the same object or value.It has effect on Value type

func add(inout input:Int){
    input++
}

var a = 10
print(a)//10
add(&a)
print(a)//11

这篇关于斯威夫特如何“通过价值”一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆