I returned to an old Cocoa project.
Written in Swift 2… by someone very helpful and knowledgeable (he will recognize himself)
So I found a Mac to load XCode 8 to convert first to Swit3.
I stumble on a few lines
1. Deprecation of DynamicType
Swift2 code was:
In a class declared as:
@objc(AAPLLoopLayout)
class AAPLLoopLayout: AAPLSlideLayout {
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes? {
guard let count = self.collectionView?.numberOfItemsInSection(0) where count != 0 else { return nil }
let itemIndex = indexPath.item
let angleInRadians = (CGFloat(itemIndex) / CGFloat(count)) * (2.0 * CGFloat(M_PI))
var subviewCenter: NSPoint = NSPoint()
subviewCenter.x = loopCenter.x + loopSize.width * cos(angleInRadians)
subviewCenter.y = loopCenter.y + loopSize.height * sin(2.0 * angleInRadians)
let itemFrame = NSMakeRect(subviewCenter.x - 0.5 * itemSize.width, subviewCenter.y - 0.5 * itemSize.height, itemSize.width, itemSize.height)
let attributes = self.dynamicType.layoutAttributesClass().layoutAttributesForItemWithIndexPath(indexPath) as! NSCollectionViewLayoutAttributes
attributes.frame = NSRectToCGRect(itemFrame)
attributes.zIndex = indexPath.item
return attributes
}
Line 12 does not compile, converter proposes
let attributes = type(of: self).layoutAttributesClass().layoutAttributesForItemWithIndexPath(indexPath) as! NSCollectionViewLayoutAttributes
But gives an error:
value of type AnyClass has no member layoutAttributesForItemWithIndexPath
I've tried a lot of variations, no success, because I search uphazardly in fact…
2. Use of unsafePointer
In a class defined as:
@objc(AAPLFileTreeWatcherThread)
class AAPLFileTreeWatcherThread: NSThread {
override func main() {
autoreleasepool {
// Create our fsEventStream.
var context: FSEventStreamContext = FSEventStreamContext()
context.version = 0
context.info = UnsafeMutablePointer(unsafeAddressOf(self).toOpaque()) // toOpaque added by converter
context.retain = nil
context.release = nil
context.copyDescription = nil
fsEventStream = FSEventStreamCreate(kCFAllocatorDefault, AAPLFileTreeWatcherEventStreamCallback, &context, paths, FSEventStreamEventId(kFSEventStreamEventIdSinceNow), 1.0, FSEventStreamCreateFlags(kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagWatchRoot | kFSEventStreamCreateFlagIgnoreSelf))
if fsEventStream != nil {
// Schedule the fsEventStream on our thread's run loop.
let runLoop = NSRunLoop.currentRunLoop()
let cfRunLoop = runLoop.getCFRunLoop()
FSEventStreamScheduleWithRunLoop(fsEventStream, cfRunLoop, CFRunLoop.Mode.commonModes)
// Open the faucet.
FSEventStreamStart(fsEventStream)
// Run until we're asked to stop.
while !self.cancelled {
runLoop.runUntilDate(NSDate(timeIntervalSinceNow: 0.25))
}
// Shut off the faucet.
FSEventStreamStop(fsEventStream)
// Unschedule the fsEventStream on our thread's run loop.
FSEventStreamUnscheduleFromRunLoop(fsEventStream, cfRunLoop, CFRunLoop.Mode.commonModes)
// Invalidate and release fsEventStream.
FSEventStreamInvalidate(fsEventStream)
FSEventStreamRelease(fsEventStream)
fsEventStream = nil
}
}
}
I get error on line 7: Cannot invoke initializer for type 'UnsafeMutablePointer<_> with an argument list of type (UnsafeMutableRawPointer)
Changing to
context.info = UnsafeMutableRawPointer(unsafeAddressOf(self)).toOpaque())
it compiles but it crashes at runtime.
and lines 17 and 31:
Cannot convert value of type CFRunLoopModel to expected argument of type CFString.
Changing to
FSEventStreamScheduleWithRunLoop(fsEventStream, cfRunLoop, CFRunLoop.Mode.commonModes as! CFString)
cleared the error. Is that correct ?
3. NSCopying
In a class
@objc(AAPLImageFile)
class AAPLImageFile: NSObject {
I have var declarations:
@NSCopying var url: NSURL
@objc dynamic var fileType: String?
var fileSize: UInt64 = 0
@NSCopying var dateLastUpdated: NSDate?
Which give error line 2 and 4:
@NSCopying is only valid with types that conform to NSCopying protocol.
Do I still need to declare @NSCopying ? Error disappears if I remove it, but may that cause other problems ?
Thanks for any help.