我仍然试图将我们的应用程序从Swift 2转换为Swift 3,因为我被迫,因为我们所有的Apple设备现在都在运行iOS 10.
我已经完成了代码转换,并认为我做得很好,但在尝试调试我的JSON问题时(在另一个问题中发布),我现在处理的请求甚至没有被发送.
let params: [String:AnyObject] = [
"email":"\(self.preferences.string(forKey: "preference_email")!)" as AnyObject
]
let requestParams: [String:AnyObject] = [
"action":"601" as AnyObject,
"params":params as AnyObject
]
do {
let requestObject = try JSONSerialization.data(withJSONObject: requestParams, options:[])
var request = URLRequest(url: URL(string: "http://domain.tld/path/")!)
request.httpBody = requestObject
request.httpMethod = "POST"
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
NSLog("Got here?")
session.dataTask(with: request) {data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
NSLog("Got here …
Run Code Online (Sandbox Code Playgroud) 下载Xcode 8并迁移到Swift 3后,我无法再归档项目.同时,项目建立没有任何问题.
我得到的错误:
体系结构armv7的未定义符号:
"Swift.UnsafeMutableBufferPointer.(subscript.materializeForSet:(Swift.Int) - > A).(closure#1)",引自:Swift.UnsafeMutableBufferPointer的泛型特化的函数签名特化:Swift.MutableCollection在Swift和Swift.UnsafeMutableBufferPointer:Swift中的Swift.RandomAccessCollection> of Swift._siftDown(inout A,index:A.Index,subRange:Swift.Range,by:inout(A.Iterator.Element,A.Iterator.Element) - > Swift.Bool) - >()在OrderCoordinator.o函数签名中使用Swift.UnsafeMutableBufferPointer进行泛型特化:WSift中的Swift.MutableCollection和Swift.UnsafeMutableBufferPointer:Swift中的Swift.RandomAccessCollection> Swift._heapSort(inout A,subRange: Swift.Range,by:inout(A.Iterator.Element,A.Iterator.Element) - > Swift.Bool) - >()在OrderCoordinator.o函数签名中使用Swift.UnsafeMutableBufferPointer进行泛型特化:Swift.Mu swift和Swift.UnsafeMutableBufferPointer中的tableCollection:Swift中的Swift.RandomAccessCollection> Swift._partition(inout A,subRange:Swift.Range,by:inout(A.Iterator.Element,A.Iterator.Element) - > Swift.Bool) - > OrderCoordinator.o中的A.Index:未找到架构armv7 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)
通过在以下函数中注释数组排序代码,我能够摆脱错误:
func didFinishWithResults(_ results: [PhotoProcessorResult]) {
guard let album = albumService.currentAlbum else { return }
//let sortedResults = results.sorted(by: { $0.fileIndex < $1.fileIndex })
let updateItems = zip(sortedResults, album.assetItems).map { (photoProcessorResult, assetItem) -> UpdateItem in
UpdateItem(path: photoProcessorResult.filePath, position: photoProcessorResult.fileIndex, isCover: assetItem.isCover)
}
albumService.updateAlbumWithItems(updateItems) { (success, …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
func completeLoadAction(urlString:String) -> Int {
let url = URL(string:urlString.trimmingCharacters(in: .whitespaces))
let request = URLRequest(url: url!)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
let ac = UIAlertController(title: "Unable to complete", message: "The load has been added to the completion queue. This will be processed once there is a connection.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
return
} …
Run Code Online (Sandbox Code Playgroud) 我面临着一个奇怪的问题MKMapView
.我用了一个MKOverlayRenderer
.现在的问题是当我缩小图像显示正确时.但是在放大的情况下,图像的某些部分会被切断.它看起来像MapView
覆盖层上方的一部分.以下是我的叠加渲染器代码.
class MapOverlayRenderer: MKOverlayRenderer {
var overlayImage: UIImage
var plan: Plan
init(overlay: MKOverlay, overlayImage: UIImage, plan: Plan) {
self.overlayImage = overlayImage
self.plan = plan
super.init(overlay: overlay)
}
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
let theMapRect = overlay.boundingMapRect
let theRect = rect(for: theMapRect)
// Rotate around top left corner
ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));
// Draw the image
UIGraphicsPushContext(ctx)
overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
UIGraphicsPopContext();
}
func degreesToRadians(_ x:Double) -> Double { …
Run Code Online (Sandbox Code Playgroud) 我是Realm的新手并且遇到了这个问题.
我有Dictionary
这样的
{
firstName : "Mohshin"
lastName : "Shah"
nickNames : ["John","2","3","4"]
}
Run Code Online (Sandbox Code Playgroud)
和class
这样
class User: Object {
var firstName: String?
var lastName: String?
var nickNames: [String]?
}
Run Code Online (Sandbox Code Playgroud)
当我尝试插入值时,它会抛出异常,如下所示
属性"nickNames"声明为"NSArray",它不是受支持的RLMObject属性类型.所有属性必须是原语NSString
,NSDate
,NSData
,NSNumber
,RLMArray
,RLMLinkingObjects
,或子类RLMObject
.
有关更多信息,请参阅https://realm.io/docs/objc/latest/api/Classes/RLMObject.html.
我也试过了
var nickNames = NSArray()
var nickNames = NSMutableArray()
Run Code Online (Sandbox Code Playgroud)
但是没有工作.我是否需要制作Nickname模型类并创建一个属性如下,或者有一种方法可以做到这一点?
var nickNames = List<Nickname>()
Run Code Online (Sandbox Code Playgroud) 我需要使用RxSwift选择集合视图中特定索引处的项目.此方法无法正常工作.
collectionView.rx.modelSelected(SearchResult.self).subscribe(onNext:{ menuItem in }).addDisposableTo(disposeBag)
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我一直在使用NSLock
s来同步敏感的代码部分,但由于它们必须从锁定它们的同一个线程中解锁而一直遇到问题.然后我发现GCD DispatchSemaphore
似乎做了同样的事情,增加了方便性,可以从任何线程发出信号.不过,我想知道,如果这种便利是以线程安全为代价的.更换是否可取
let lock = NSLock()
lock.lock()
// do things...
lock.unlock()
Run Code Online (Sandbox Code Playgroud)
同
let semaphore = DispatchSemaphore(value: 1)
semaphore.wait()
// do things...
semaphore.signal()
Run Code Online (Sandbox Code Playgroud)
或者我会遇到有关线程安全的问题吗?
我已经阅读了Apple的Swift iBook(类型转换和协议)的相关部分,但我似乎找到了一种方法来指定对象是符合特定协议的特定类的实例.
作为一个例子tableView(_: , cellForRowAt: )
我希望将由返回的单元格转换tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath)
为UITableViewCell
符合RLMEntityCapableCell
协议的子类(只是指定构造item
函数有一个变量,称为该实例Object
,或其子类之一).
这条路线有效,但双重铸造似乎过度:
protocol RLMEntityCapableCell: class {
var item: Object { get set }
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) as! RLMEntityCapableCell // Cast here so we can set item
cell.item = items[indexPath.row]
return cell as! UITableViewCell // Cast again so the return type is right… …
Run Code Online (Sandbox Code Playgroud) 在一个单元格中,我想在Parse服务器中显示NSDate的时间.这是代码,但它不起作用.什么都没有改变,数据没有被解析.
if let createdat = (object?["createdAt"] as? String){
let pastDate = Date(timeIntervalSinceNow: TimeInterval(createdat)!)
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}
extension Date {
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
if secondsAgo < minute {
return "\(secondsAgo) sec ago"
} else if secondsAgo < hour {
return "\(secondsAgo / minute) min ago"
} else if secondsAgo < day { …
Run Code Online (Sandbox Code Playgroud) 我想开发一个应用程序,以促进在Mac上使用终端时的体验.我需要从终端获取当前工作目录(cwd).我该如何实现它?我注意到在命令swift之后获取终端输出的答案真的很好,但它似乎仍然无法完美地解决我的问题.我从Apple的文档https://developer.apple.com/reference/foundation/process中学习了Process().currentDirectoryPath 它能做到我需要的技巧吗?我可以像下面一样使用它吗?
let path = Process().currentDirectoryPath
Run Code Online (Sandbox Code Playgroud)
我是swift和Xcode的新手,请帮助我!谢谢!
更新:谢谢你们!似乎两者都有
let path = fileManager.default.currentDirectoryPath
Run Code Online (Sandbox Code Playgroud)
和Process()暂时为我工作.这两者之间有什么不同吗?
swift3 ×10
ios ×6
swift ×6
xcode ×2
cgcontext ×1
concurrency ×1
http ×1
macos ×1
mkmapview ×1
mkoverlay ×1
nsurlrequest ×1
nsurlsession ×1
parse-server ×1
realm ×1
rx-swift ×1
xcode8 ×1