小编OmO*_*ker的帖子

在 Swift 中将多部分表单数据添加到 uploadTask(with:fromfile:)

我正在尝试将文件(png、doc、jpg 等)发送到我的服务器。但是,为了正确保存文件,我想将文件扩展名添加到我的 POST 请求中。Apple.developer 说“忽略此请求对象中的正文流和正文数据。” 有没有办法将正文数据添加到我的请求中?这是我到目前为止所拥有的:

func uploadFile(filePath: String, endpoint: String) -> Void {

    let session = URLSession.shared
    let url = URL(string: endpoint)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let fileUrl = URL(fileURLWithPath: filePath)

    let task = session.uploadTask(with: request, fromFile: fileUrl)
    task.resume()

}
Run Code Online (Sandbox Code Playgroud)

我尝试切换到使用 uploadTask(with:data:) 但我不确定如何将文件添加到数据,因为大多数示例都使用图像

macos file-upload multipartform-data http-post swift

6
推荐指数
0
解决办法
231
查看次数

python递归中的LinkedLists

我在python中有一个简单的LinkedList实现.如何在方法中使用递归?我知道递归是如何工作的,但我如何使用自我递归.如果有人可以修复我的代码,但我对解释更感兴趣,那么我可以在不同的方法中使用它.

LinkedList代码:

class Node:
    def __init__(self, item, next):
        self.item = item
        self.next = next

class LinkedList:
    def __init__(self):
        self.head = None

    def add(self, item):
        self.head = Node(item, self.head)

    def remove(self):
        if self.is_empty():
            return None
        else:
            item = self.head.item
            self.head = self.head.next
            return item

    def is_empty(self):
        return self.head == None 
Run Code Online (Sandbox Code Playgroud)

我的代码是:

def count(self, ptr=self.head):
    if ptr == None:
        return '0'
    else:
        return 1 + self.count(ptr.next)
Run Code Online (Sandbox Code Playgroud)

它给了我一个错误:

def count(self, ptr=self.head):
NameError: name 'self' is not defined
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢.

python recursion linked-list

3
推荐指数
1
解决办法
364
查看次数