Alamofire 4文件上传参数问题(Alamofire 4 File Upload Parameter issue)
请看看邮递员POSTMAN成功结果的图片,但代码无法运行我错过了一些东西,请帮助我
var parameters = [String: String]() parameters = [ "profile_image": "imagefile" ] let headers: HTTPHeaders = [ "Accept": "application/json" ] let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx" let completeUrl = URL(string:url)! let imageData = UIImageJPEGRepresentation(chosenImage!, 1) print ("image data:: \(imageData)") print ("chosenImage:: \(chosenImage)") // Alamofire.upload(imageData!, to: completeUrl).response { response in // print (response) // } Alamofire.upload( multipartFormData: { multipartFormData in multipartFormData.append(imageData!, withName: "imagefile", fileName: "image.jpg", mimeType: "image/jpeg") for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key) } }, to: completeUrl, headers: headers, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON{ response in print(response) } case .failure(let encodingError): print(encodingError) } } )
邮差回复成功的邮差,但有头问题,你能指出我犯的愚蠢的错误
Plese have a look at the pic here from postman POSTMAN success result but the code does not run I am missing something please help me out
var parameters = [String: String]() parameters = [ "profile_image": "imagefile" ] let headers: HTTPHeaders = [ "Accept": "application/json" ] let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx" let completeUrl = URL(string:url)! let imageData = UIImageJPEGRepresentation(chosenImage!, 1) print ("image data:: \(imageData)") print ("chosenImage:: \(chosenImage)") // Alamofire.upload(imageData!, to: completeUrl).response { response in // print (response) // } Alamofire.upload( multipartFormData: { multipartFormData in multipartFormData.append(imageData!, withName: "imagefile", fileName: "image.jpg", mimeType: "image/jpeg") for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key) } }, to: completeUrl, headers: headers, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON{ response in print(response) } case .failure(let encodingError): print(encodingError) } } )
Postman Response Success on postman but having issue with headers, can you point out the silly mistake i have make
原文:https://stackoverflow.com/questions/42935529
最满意答案
有许多可能的算法加速。 一些基本的可能是:
- 首先,如果你只对最大的素质因素感兴趣,你应该从最大的可能性中检查它们,而不是最小的。 因此,不要从
2
循环到a/2
尝试从 downto2
进行检查。- 您可以加载素数据库而不是使用
isprime
函数(网络中有几十个这样的文件)- 此外,只有奇数可以是素数(
2
除外),因此您可以在每次迭代中“跳跃”2个值您的
isprime
检查器也可以突出显示,您不必查找高达b/2
divisiors,只需检查sqrt(b)
,从而将复杂度从O(n)
降低到O(sqrt(n))
(假设模运算是恒定时间)。There are many possible algorithmic speed ups. Some basic ones might be:
- First, if you are only interested in the largest prime factor, you should check for them from the largest possible ones, not smallest. So instead of looping from
2
toa/2
try to check froma
downto2
.- You could load the database of primes instead of using
isprime
function (there are dozens of such files in the net)- Also, only odd numbers can be primes (except for
2
) so you can "jump" 2 values in each iterationYour
isprime
checker could also be speededup, you do not have to look for divisiors up tob/2
, it is enough to check tosqrt(b)
, which reduces complexity fromO(n)
toO(sqrt(n))
(assuming that modulo operation is constant time).
相关问答
更多-
实际上,有几种更有效的方式来找出大数量的因素(对于较小的试验部门的工作相当好)。 如果输入数字具有非常接近于其平方根的两个因素,那么非常快的一种方法被称为费马因子分解 。 它利用身份N =(a + b)(a - b)= a ^ 2 - b ^ 2,易于理解和实现。 不幸的是,它一般不是很快。 二进制筛是最常用的将数字分配到100位数的方法。 作为一个奖励,算法的一部分很容易通过并行处理完成。 我听说过的另一种算法是Pollard的Rho算法 。 它并不像二次筛一般有效,但似乎更容易实现。 一旦你决定如何将一 ...
-
最大的Prime因子-C ++(Largest Prime Factor- C++)[2023-07-28]
num = 600851475143; 这里发生整数溢出。 num的大小不足以包含您提供的值。 使用uint64_t 。 #include//must include this! uint64_t num = 600851475143; 阅读本文: cstdint num = 600851475143; Integer overflow occurs here. The size of num is not large enough to contain the value ... -
我真的没有得到你想要完成的东西。 就这么简单: def largestPrimeFactor(b : BigInt) = { def loop(f:BigInt, n: BigInt): BigInt = if (f == n) n else if (n % f == 0) loop(f, n / f) else loop(f + 1, n) loop (BigInt(2), b) } 虽然这里没有优化,但我立刻得到了结果。 唯一的“技巧”是你必须知道一个数字的最 ...
-
当它真的被翻译成Prolog时,一个天真的强力搜索仍然是一个天真的强力搜索。 没关系:Plain Prolog非常适合描述和运行强力搜索。 不过,用Prolog编写的暴力搜索,你可能无法击败手动优化的任何蛮力搜索的低级版本。 另一方面,Prolog非常简单方便,你很可能很快就会找到更好的搜索策略,并且有一些原型,而这些其他策略通常会很容易地以巨大的优势击败任何暴力破解。 即使翻译得天真,Prolog在回溯方面也非常出色,并且在较低级代码的某些(更小或更大,取决于您使用的Prolog实现)因素中有效地执行。 ...
-
数字600851475143超出了可以用PHP中的整数表示的范围,因此当您使用32位计算机时, 600851475143 / 6857不是整数: php> var_dump(600851475143/6857); float(87625999) 如果您使用BC任意精度库中的bcmod实现可分性检查,则可以使其工作: php> echo bcmod('600851475143','6857'); 0 在你的代码中: for ($i=$test; $i > 1 ; $i--) { if(bcmod ...
-
public static void main(String[] args) { int array[]= new int[100]; { int index=0; for(int i=0;i<100;i++) { int m; m=search(i); if(m==1||m==2 || m==3 || m==5){ array[index++]=i; } } }} 看看。我希望它会有所帮助。 public static void main(String[] args) { int array[ ...
-
有许多可能的算法加速。 一些基本的可能是: 首先,如果你只对最大的素质因素感兴趣,你应该从最大的可能性中检查它们,而不是最小的。 因此,不要从2循环到a/2尝试从 downto 2进行检查。 您可以加载素数据库而不是使用isprime函数(网络中有几十个这样的文件) 此外,只有奇数可以是素数( 2除外),因此您可以在每次迭代中“跳跃”2个值 您的isprime检查器也可以突出显示,您不必查找高达b/2 divisiors,只需检查sqrt(b) ,从而将复杂度从O(n)降低到O(sqrt(n)) (假设模运 ...
-
几乎和蛮力一样:计算一个md5sum是微不足道的,但是使一个文件匹配一个已知的md5sum是很困难的。 在最好的情况下,你正在寻找一个能够给你一个已知散列的预映像(可能通过将数据添加到二进制文件中)。 Pretty much the same as brute force: computing a md5sum is trivial, but making a file to match a known md5sum is hard. You are, in the best case, looking f ...
-
数字的主要因素(prime factor of a number)[2022-12-25]
每个数字都可以表示为: x = 1 p 1 .a 1 p 1 ... a n p n 哪里: a i 1 <= i <= n是素因子。 因此,为了使x尽可能最小,所有因子应为2 ,因此x将为2 ^ f(x) 。 x的除数是:(p 1 + 1)(p 2 + 1)...(p n + 1)所以你的附加条件也会得到满足! Every number can be represented in the form of: x = a1p1.a1p1...anpn Where: ai 1 <= i <= n are pri ... -
n=600851475143 d=2 while d < n/d: if n%d==0: n/=d else: d+=1 print n n=600851475143 d=2 while d < n/d: if n%d==0: n/=d else: d+=1 print n