我可以将一个类传递给 Swift 上同一个类中的一个函数吗(尝试编写一个使用 C++ 和 Swift 语言编写的类)

Posted

技术标签:

【中文标题】我可以将一个类传递给 Swift 上同一个类中的一个函数吗(尝试编写一个使用 C++ 和 Swift 语言编写的类)【英文标题】:Can i pass a class into a function within the same class on Swift (Trying to write a class that was written using C++ with the Swift Language) 【发布时间】:2018-04-07 19:04:05 【问题描述】:

不久前,我创建了一个程序来对 C++ 进行一些计算,我正在尝试使用 Swift 执行相同的基本程序,作为开始学习语言 (Swift) 的一种方式,但 XCode 一直给我关于我试图访问的数组中的变量。基本上,我试图从我在类本身中创建的类的数组中访问一个变量。我真的不太擅长解释,但我会向您展示我尝试使用 swift 重新创建的 C++ 类:

class investors 
    string name = " "; //Name of Investor
    double investment = 0; // Amount Invested
    double last_total = 0; // current total value of all investments(execluding this investment) just before adding the investment.
    double percent = 0; // Percentage

    public:
    // Set and Get Functions for Values
    void set_name(string new_name)             name = new_name;
    void set_investment(double new_investment) investment = new_investment;
    void set_last_total(double new_last_total) last_total = new_last_total;
    void set_percent(double new_percent)       percent = new_percent;

    string get_name()       return name;
    double get_investment() return investment;
    double get_last_total() return last_total;
    double get_percent()    return percent;

    void recalc_percent(vector <investors> &old_investors)  // Function that recalculates all previous percentage to account for the added investment
        for (int i = 0; i < old_investors.size(); i++) 
            double temp_percent = old_investors.at(i).get_percent();
            temp_percent = (temp_percent * last_total) / (last_total + investment);
            old_investors.at(i).set_percent(temp_percent);
        
    

    void calc_percent(vector <investors> &old_investors)  // Calculating Newly added percentage of investment added 
        percent = (investment / (investment + last_total)) * 100;
        recalc_percent(old_investors);
    

    double current_amount(double final_amount)  // Returns how much investor currently has in US Dollars
        return (percent * final_amount) / 100;
    
;

这就是我使用 swift 的地方:

class Investors 
    var name: String
    var investment = 0.0
    var lastTotal = 0.0
    var percent = 0.0

    init(name: String, investment: Double, lastTotal: Double, percent: Double) 
        self.name = name
        self.investment = investment
        self.lastTotal = investment
        self.percent = percent
    

    func recalc_percent(oldInvestors: [Investors])    
        for (people) in oldInvestors 
            var tempPercent = oldInvestors[people].percent // This is where my issue is.
        
    

    func calc_percent(oldInvestors: [Investors])
        percent = (investment / (investment + lastTotal)) * 100 //I had an error over here as well.
    

Swift 有什么限制?我认为这些限制意味着我必须以不同于 C++ 版本的方式编写程序。

编辑:swift 代码中的第一个错误(我在发生错误的代码旁边评论)给了我这个消息:Cannot subscript a value of type [Investors] with an index of type Investors.

稍后我会添加第二条错误消息。

【问题讨论】:

您的问题标题似乎与您的正文问题不匹配,请进行编辑以更清楚您的实际问题。 嗨@aimen,您还应该尝试创建minimal reproducible example,并显示您遇到的错误。这将帮助其他人帮助您,并使您的问题将来对其他人更有用。欢迎来到 SO。 会做的,谢谢您的建议 【参考方案1】:

您似乎没有完全理解 for 循环如何在 swift 中工作。在这样的 for 循环中:

for (people) in oldInvestors


people 在此处每次迭代都会包含对数组中每个元素的引用(指针)。也就是说,这里的变量people其实和C++代码中的old_investors.at(i)类似。

这就是为什么使用people 作为下标不起作用的原因。

我已经为你翻译了剩下的代码:

class Investors

    var name: String
    var investment = 0.0
    var lastTotal = 0.0
    var percent = 0.0

    init(name: String, investment: Double, lastTotal: Double, percent: Double) 
        self.name = name
        self.investment = investment
        self.lastTotal = investment
        self.percent = percent
    

    func recalc_percent(oldInvestors: [Investors])

        for investor in oldInvestors
            var tempPercent = investor.percent
            investor.percent = (tempPercent * lastTotal) / (lastTotal + investment)

        


    

    func calc_percent(oldInvestors: [Investors])
        percent = (investment / (investment + lastTotal)) * 100 

        recalc_percent(oldInvestors: oldInvestors)
    

    func currentAmount(finalAmount: Double) -> Double 
        return (percent * finalAmount) / 100
    

虽然你正在尝试做的事情很奇怪......

我建议你先阅读 Apple 的 Swift Guide 上的所有内容,然后自己编写一些 swift 程序,在翻译代码之前进入 swift 思维模式。

【讨论】:

非常感谢!我已经完成了其中的一小部分,但我认为我可以开始做一些简单的事情,我倾向于在做某事之后比之前学得更快。 @aimenalt 如果您认为我的回答回答了您的问题,请考虑通过单击该复选标记接受它! 计算并重新计算每个人向投资罐基金投资的金额和百分比。假设有人投资了 5000,然后增长到 6000,然后其他人投资了 5000,两者投资相同,但早期的人的钱已经增长了一点。所以我的主要问题是每次添加新投资者时,我都必须重新计算每个人的当前金额/增加和百分比。当添加另一个人时,它变得令人头疼,并且达到了我需要 2 个小时才能重新计算的地步。所以我创建了一个程序来为我重新计算

以上是关于我可以将一个类传递给 Swift 上同一个类中的一个函数吗(尝试编写一个使用 C++ 和 Swift 语言编写的类)的主要内容,如果未能解决你的问题,请参考以下文章

如何将NSNib.Name传递给Swift / Cocoa中的窗口控制器

将变量传递给另一个 JAVA 类中的主函数

在类之间存储和传递数据的正确方法

将数据从委托 Swift 类传递到 SwiftUI 结构中的 EnvironmentObject

单击按钮后如何将输入值(TKinter)传递给其他类中的函数并使用传递的值运行函数[重复]

将变量传递给main方法java