Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create UnsafeMutablePointer<CGPoint> Object in swift

I need to call the UIScrollViewDelegate methods I don't know how to create UnsafeMutablePointer object.

let pointer:UnsafeMutablePointer<CGPoint>  = CGPoint(x: 0, y: 1) as! UnsafeMutablePointer<CGPoint>

self.scrollViewWillEndDragging(self.collectionView, withVelocity: CGPoint(x: 0, y: 1), targetContentOffset: pointer)

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{  
     //Some code here
}
like image 986
Rajesh Kumar Avatar asked Feb 10 '16 07:02

Rajesh Kumar


1 Answers

import Foundation
var point = CGPoint(x: 10.0, y: 20.0)
let p = withUnsafeMutablePointer(&point) { (p) -> UnsafeMutablePointer<CGPoint> in
    return p
}

print(p.memory.x, p.memory.y) // 10.0 20.0
point.x = 100.0
print(p.memory.x, p.memory.y) // 100.0 20.0

with help of Swift's syntactic sugar

import Foundation
var point = CGPoint(x: 10.0, y: 20.0)
let p = withUnsafeMutablePointer(&point) { $0 }

print(p.dynamicType) // UnsafeMutablePointer<CGPoint>
like image 180
user3441734 Avatar answered Nov 15 '22 05:11

user3441734