Skip to content

Instantly share code, notes, and snippets.

@roothybrid7
Created July 29, 2015 00:20
Show Gist options
  • Save roothybrid7/4d241cb5050f60f7ca17 to your computer and use it in GitHub Desktop.
Save roothybrid7/4d241cb5050f60f7ca17 to your computer and use it in GitHub Desktop.
Swift Guard Statement利用ケース ref: http://qiita.com/roothybrid7/items/beba0daf9bf7338f4874
class CustomCollectionViewLayout : UICollectionViewFlowLayout {
var minimumLineSpacing: CGFloat = 0.0
var minimumInteritemSpacing: CGFloat = 0.0
var itemSize: CGSize = CGSizeMake(50, 50)
}
extension CustomCollectionViewLayout {
func minimumLineSpacingForSectionAtIndex(section: Int) -> CGFloat {
guard let collectionView = self.collectionView, delegate = self.delegate else {
return 0.0
}
guard let spacing = delegate.collectionView?(collectionView, layout: self, minimumLineSpacingForSectionAtIndex: section) else {
return self.minimumLineSpacing
}
return spacing
}
func minimumInteritemSpacingForSectionAtIndex(section: Int) -> CGFloat {
guard let collectionView = self.collectionView, delegate = self.delegate else {
return 0.0
}
guard let spacing = delegate.collectionView?(collectionView, layout: self, minimumInteritemSpacingForSectionAtIndex: section) else {
return self.minimumInteritemSpacing
}
return spacing
}
func insetForSectionAtIndex(section: Int) -> UIEdgeInsets {
guard let collectionView = self.collectionView, delegate = self.delegate else {
return UIEdgeInsetsZero
}
guard let inset = delegate.collectionView?(collectionView, layout: self, insetForSectionAtIndex: section) else {
return self.sectionInset
}
return inset
}
func sizeForItemAtIndexPath(indexPath: NSIndexPath) -> CGSize {
guard let collectionView = self.collectionView, delegate = self.delegate else {
return CGSizeZero
}
guard let size = delegate.collectionView?(collectionView, layout: self, sizeForItemAtIndexPath: indexPath) else {
return self.itemSize
}
return size
}
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 2.0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 2.0
}
func (g *Gopher) WriteTo(w io.Writer) (size int64, err error) {
err = binary.Write(w, binary.LittleEndian, int32(len(g.Name)))
if err != nil {
return
}
size += 4
n, err := w.Write([]byte(g.Name))
size += int64(n)
if err != nil {
return
}
err = binary.Write(w, binary.LittleEndian, int64(g.AgeYears))
if err == nil {
size += 4
}
return
}
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(["name": "John"])
// prints "Hello John!"
// prints "I hope the weather is nice near you."
greet(["name": "Jane", "location": "Cupertino"])
// prints "Hello Jane!"
// prints "I hope the weather is nice in Cupertino."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment