[英]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)
你好 Stack Overflow 的人們! 這將是我第一次發帖,我對編程相對較新,去年年初才開始使用 C++,不久前我創建了一個程序來對 C++ 進行一些計算,我正在嘗試做與 Swift 相同的基本程序作為開始學習語言 (swift) 的一種方式,但 xcode 不斷給我關於我試圖訪問的數組中的變量的錯誤,基本上我試圖從類的數組中訪問變量 i我在類本身內部創建。 我真的不太擅長解釋,但我會向您展示我嘗試使用 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 代碼中的第一個錯誤(我在發生錯誤的代碼旁邊進行了評論)給了我這樣的消息:無法使用類型為“投資者”的索引對“[投資者]”類型的值進行下標
稍后我將添加第二條錯誤消息。
您似乎沒有完全理解 swift 中的 for 循環是如何工作的。 在這樣的 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思維模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.