gpt4 book ai didi

swift2 - swift : Instance member cannot be used on type

转载 作者:行者123 更新时间:2023-12-01 15:05:13 25 4
gpt4 key购买 nike

我试图从我的 Data 类中的 Users 类调用一个变量,但我抛出了这个错误

Instance member 'user' cannot be used on type 'Data'

数据类代码:

import Foundation

class Data {

static let sharedInstance = Data()

let user = Users()
var id = (user.userId).string

//Login.swift
static let quarterlyEndpoint: String = "http://anyapi/api/users/\(id)/quarterly/tasks"

// for prevent from creating this class object
private init() { }

}

Users类代码:

var userId: Int {

guard let _userId = getId() else {
return 0
}

return _userId
}

更新

我可以调用函数,但是当我打印 weeklyEnd 时,我会得到 http://anyapi/api/users/(Function)/quarterly/tasks 而不是http://anyapi/api/users/3/quarterly/tasks

let user = Users()

func getId() -> String
{
let id = String(user.userId)
return id
}

static let weeklyEndpoint: String = "http://anyapi/api/users/\(getId)/quarterly/tasks"

更新 2

import Foundation

class Data {

static let sharedInstance = Data()

let user = Users()
let id:String?
let weeklyEndpoint: String?

// for prevent from creating this class object
private init() {
id = String((user.userId))
//Login.swift
weeklyEndpoint = "http://anyapi/api/users/\(id)/quarterly/tasks"
}

}

我在从 Data 类外部调用我的 weeklyEndPoint 时遇到问题。我试过 Data.weeklyEndPoint 但它不起作用

最佳答案

尝试像这样在 Data 类中创建一个方法。

func getusrid ()-> String
{
var id = (user.userId).string
return id ;

}

你不能在任何方法之外从实例变量中获取值

编辑:

class Data {

static let sharedInstance = Data()
let id:String?
let quarterlyEndpoint: String?

let user = Users()
private init()
{
id = String((user.userId))
quarterlyEndpoint = "http://anyapi/api/users/\(id)/quarterly/tasks"

}




}

关于swift2 - swift : Instance member cannot be used on type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37475329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com