esqarrouth / ezswiftextensions Goto Github PK
View Code? Open in Web Editor NEW:smirk: How Swift standard types and classes were supposed to work.
License: MIT License
:smirk: How Swift standard types and classes were supposed to work.
License: MIT License
Add new Array and Dictionary extensions into readme with examples
@lfarah docs pls
Hi,
When trying to run UIFont.HelveticaNeue(type: .Regular, size: 13)
getting: fatal error: unexpectedly found nil while unwrapping an Optional value
trimming string means removing special characters from start and end of string. trim() removes whitespace characters from inside string
e.g:
let string = " Hello how are you "
let` trimmedStr = string.trimmed() // = '"Hellohowareyou"
While expected result is "Hello how are you"
Missing documentation for these methods:
///EZSE: Returns bold NSAttributedString
func bold() -> NSAttributedString {}
///EZSE: Returns underlined NSAttributedString
func underline() -> NSAttributedString {}
///EZSE: Returns italic NSAttributedString
func italic() -> NSAttributedString {}
///EZSE: Returns NSAttributedString
func color(color: UIColor) -> NSAttributedString {}
Hi, I just thought whether it would be great to have Slack/Gitter so we could simplify communication of developing, improving this library.
Well, I suppose this will involve some new people to help improve library.
Maybe, just give a try. What do you think? Thumbs up/down, please :)
add please at first line
guard characters.count > 0 else { return self }
because String().capitalizeFirst crashes the program. If someone would like to check on client side it's good but if someone not, it's not the expected behaviour to crash the program
Now it looks like the following
let filterHeaderView = UINib(nibName: FilterHeaderView.className, bundle: nil).instantiateWithOwner(self, options: nil).last as? FilterHeaderView
Not as Swifty as it could be. Should we have an extension for that?
Well I don't actually know yet how it could be more swifty. Could anybody give an example? Something like let filterHeaderView = UINib.instantiate(view: FilterHeaderView)
- loads nib from nil bundle with class name name, searches for view with passed class and returns it. Or something more controllable.
capitalizeFirst
inside StringExtensions
realizes an action of capitalizing the first character of a string, so therefore it should be a method instead of a variable with a getter.
Today's implementation:
/// EZSE: Capitalizes first character of String
public var capitalizeFirst: String {
guard characters.count > 0 else { return self }
var result = self
result.replaceRange(startIndex...startIndex, with: String(self[startIndex]).capitalizedString)
return result
}
What it should be:
/// EZSE: Capitalizes first character of String
public func capitalizeFirst() -> String {
guard characters.count > 0 else { return self }
var result = self
result.replaceRange(startIndex...startIndex, with: String(self[startIndex]).capitalizedString)
return result
}
Hi,
Making app delegate easier to access won't lead to better code, because that pattern leads to circular dependencies. Please consider removing this part.
Good job on other parts of the library!
Thanks!
I think there should be a bit more methods and properties for NSDate. First, I know EXSwift had properties for elements such as day, month, year, etc. Those were pretty useful IMO.
Second, I think there should be methods to be able to easily determine if a NSDate is on the same day, same month or same year as another date instead of just having the == operator.
What do you think?
isPositive: Bool { return (self >= 0) }
I'd like the UISwitch
to have a .toggle()
just like Booleans have a .toggle()
, so:
var myBool: Bool = true // True
myBool.toggle() // False
var mySwitch: UISwitch = UISwitch(x: 0, y: 0, w: 50, h: 50)
mySwitch.setOn(true, animated: true) // True
mySwitch.toggle() //False
extension UISlider
{
///EZSE: Slider moving to value with animation duration
public func setValue(value: Float, duration: Double) {
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(self.value, animated: true)
}) { (bol) -> Void in
UIView.animateWithDuration(duration, animations: { () -> Void in
self.setValue(value, animated: true)
}, completion: nil)
}
}
}
public mutating func removeObject<U: Equatable>(object: U) {
for i in (0..<count).reverse() {
if let obj = self[i] as? U where obj == object {
self.removeAtIndex(i)
}
}
}
The above method seems to have a type safety hole at obj = self[i] as? U
, which by the way, should have been obj = self[i] as! U
in previous versions of Swift . The API is available for arrays that are not equatable as well. This is unsafe and prone to confusion and error. Also, no exceptions are thrown if the element isn't found.
We should refactor this to:
extension Array where Element : Equatable {
public mutating func removeObject(object: Element) throws {
var index = 0
for element in self {
if object == element {
self.removeAtIndex(index)
return
}
index += 1
}
throw ArrayException.ObjectNotFound
}
}
where ArrayException
is
enum ArrayException : ErrorType {
case ObjectNotFound
}
This is just an example we should apply this to all places where we are conditionally/forcefully downcasting to a type constraint.
When called, it waits x seconds before executing for the first time. Should execute right away!
Most of the code seems un-documented. We should add an appledoc style documentation.
Normally, you can only remove a subview if you have the object. In this method, you can remove a subview by having it's x and y
What do you guys think?
extension UIView {
func removeSubview(x:Double,y:Double) {
for view in self.subviews {
if Float(view.frame.origin.x) == Float(x) && Float(view.frame.origin.y) == Float(y) {
view.removeFromSuperview()
"removed"
}
}
}
}
Please add random color function to UIColor extantion.
Before I start implementing or creating a pull request, I wanted to ask if UnderscoreJS and Lodash would be appropriate extensions to include in this Library? Here is an example of these type of Extensions already implemented in Swift, Dollar.swift.
public func addKeyboardWillShowNotification() {
self.addNotificationObserver(UIKeyboardWillShowNotification, selector: "keyboardWillShowNotification:");
}
public func addKeyboardDidShowNotification() {
self.addNotificationObserver(UIKeyboardDidShowNotification, selector: "keyboardDidShowNotification:");
}
public func addKeyboardWillHideNotification() {
self.addNotificationObserver(UIKeyboardWillHideNotification, selector: "keyboardWillHideNotification:");
}
public func addKeyboardDidHideNotification() {
self.addNotificationObserver(UIKeyboardDidHideNotification, selector: "keyboardDidHideNotification:");
}
The above and related methods are not safe and it might easily cause a crash if the developer is not careful. It's better if we remove it or use a protocol to introduce safety.
Hi, when I use carthage to import the EZSwiftExtensions, I got the the error since the project base sdk is iOS 9.0 while mine is iOS 8.0, thus I have to modify the configuration manually and rebuild, can the author modify the project sdk to 8.0 thus we can keep our framework updated. No everyone's project use minimal sdk 9.0 right?
We are building a keyboard application and we can't use the pod in it. There is a problem with the UIAlertControllerExtension
class. Here's the error:
EZSwiftExtensions/Sources/UIAlertControllerExtensions.swift:13:23: 'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.
In my opinion, there should be a different sub pod for extensions that use Application so that we can still use the others in a non-application project.
Thoughts?
Hi,
Not clear what is the real intention of this function. To remove the first found, the last found or all of the found objects? Right now it removes only the last one.
Cheers!
Nowadays, to resize an UIImage, we gotta use this:
let image = UIImage(named: "img")
let image2 = UIImage.scaleTo(image: image, w: 50, h: 50)
In the example above, we're creating another UIImage from our UIImage, which is kinda confusing. In my opinion, we can make it better:
let image = UIImage(named: "img")
image.scaleTo(w: 50, h: 50)
What do you guys think? ๐ค
When I use carthage update failure and show many warning and error below info
*** Building scheme "EZSwiftExtensions-iOS" in EZSwiftExtensions.xcworkspace
config error: 'variable_name_min_length' is not a valid rule identifier
config error: 'valid_docs' is not a valid rule identifier
A shell task failed with exit code 65:
** BUILD FAILED **
The following build commands failed:
PhaseScriptExecution Run\ Script /Users/xxx/Library/Developer/Xcode/DerivedData/EZSwiftExtensions-hdalhogyszjhlnaluqlsmltwrjbs/Build/Intermediates/EZSwiftExtensions.build/Release-iphoneos/EZSwiftExtensions-iOS.build/Script-B27008081CAA12D5002177ED.sh
(1 failure)
//
// UIColorExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIColor {
/// EZSE: init method with RGB values from 0 to 255, instead of 0 to 1. With alpha(default:1)
public convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1) {
self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
/// EZSE: init method with hex string and alpha(default: 1)
public convenience init?(hexString: String, alpha: CGFloat = 1.0) {
var formatted = hexString.stringByReplacingOccurrencesOfString("0x", withString: "")
formatted = formatted.stringByReplacingOccurrencesOfString("#", withString: "")
if let hex = Int(formatted, radix: 16) {
let red = CGFloat(CGFloat((hex & 0xFF0000) >> 16)/255.0)
let green = CGFloat(CGFloat((hex & 0x00FF00) >> 8)/255.0)
let blue = CGFloat(CGFloat((hex & 0x0000FF) >> 0)/255.0)
self.init(red: red, green: green, blue: blue, alpha: alpha) } else {
return nil
}
}
/// EZSE: init method from Gray value and alpha(default:1)
public convenience init(gray: CGFloat, alpha: CGFloat = 1) {
self.init(red: gray/255, green: gray/255, blue: gray/255, alpha: alpha)
}
/// EZSE: Red component of UIColor (get-only)
public var redComponent: Int {
var r: CGFloat = 0
getRed(&r, green: nil, blue: nil, alpha: nil)
return Int(r * 255)
}
/// EZSE: Green component of UIColor (get-only)
public var greenComponent: Int {
var g: CGFloat = 0
getRed(nil, green: &g, blue: nil, alpha: nil)
return Int(g * 255)
}
/// EZSE: blue component of UIColor (get-only)
public var blueComponent: Int {
var b: CGFloat = 0
getRed(nil, green: nil, blue: &b, alpha: nil)
return Int(b * 255)
}
/// EZSE: Alpha of UIColor (get-only)
public var alpha: Int {
var a: CGFloat = 0
getRed(nil, green: nil, blue: nil, alpha: &a)
return Int(a)
}
/// EZSE: Returns random UIColor with random alpha(default: false)
public static func randomColor(randomAlpha: Bool = false) -> UIColor {
let randomRed = CGFloat.random()
let randomGreen = CGFloat.random()
let randomBlue = CGFloat.random()
let alpha = randomAlpha ? CGFloat.random() : 1.0
return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: alpha)
}
}
private extension CGFloat {
/// SwiftRandom extension
static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
}
}
Maybe with a closure to track when its clicked?
This is our test coverage at the moment. Test Coverage
At this point, stability and optimization is more important than new features. So if you want to contribute:
I will be creating empty test files for everything so it will be easy to fill in. Ideally if a user wants to know what a function does, they should be able to just check the tests files and understand from the example code in there.
Thank you all for contributing!
Im really excited for this repo, but I am wondering if there will be Carthage support anytime soon?
let array1 = [1,2,3]
let array2 = [1,2]
array1.containsArray(array2) // return true or false
Need this extension for Array.
Thank you for the useful extensions!
We recently integrated EZSwiftExtensions into our production project and noticed some surprising breakages happening.
Removing EZSwiftExtensions fixed the issues.
We wonder if there is a subtle name clash going on with Apple internal method names since compiler did not give us any warning about conflicts.
When we ask Apple for support Apple Engineers recommended using a name prefix for extension names to their classes to avoid potential conflict.
Is there any other way to avoid name clash?
Having NSAttributedString
properties in String Extension would be useful. Some examples could be
let myStr = "Hello World"
let attrStr1 = myStr.undeline()
let attrStr2 = myStr.bold()
let attrStr3 = myStr.italic()
let attrStr4 = myStr[0...6].italic()
let attrStr5 = myStr[7...10].color(UIColor.greenColor())
let attrStr6 = myStr.link("www.google.com") // or href("www.google.com")
let attrStr7 = attrStr4.add(attrStr5).add(attrStr6)
Also chaining could be great. This would require extensions in both NSAttributedString
and String
let attrStr1 = myStr.undeline().italic().link("www.google.com")
Hi, could you push 1.4.1 to CocoaPods ?
I can not find 1.4.1 directly via "pod search"
How it is:
public convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, imageName: String) {}
How it should be:
public convenience init(frame: CGRect, imageName: String) {}
Just for testing string for not containing any useful information we need to call a 39-length method. Also I suggest to make it computable property instead of method.
Should we rename it to something like isWhitespaced
or
isSpaced
(python-alike)
isStripped
(python-alike)
isBlank
(Java-alike)
Don't know as of swift3 changes about this, but swift 2
NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
just for getting document directory is not cool and swifty. Also I haven't found any methods by searching 'document' and 'directory'.
todo uicolor hex and hexstring initilizers as
I am trying to use this example and I run this code and get the following results:
let format = "yyyy/MM/dd"
let fromString = "2016/01/11"
var date = NSDate(fromString: fromString, format: format)
print(date) // Optional("2016/01/11 00:00:00 +0000")
print(date?.toString())
Optional(2016-01-11 08:00:00 +0000)
Optional("12:00:00 AM")
I think toString() should include the whole date.
@furuyan
just like this
var selectOrders = Array<Order>()
self.orderArray.removeObjects(selectOrders)
Hey. According to Apple docs (section "Adding a Child View Controller to Your Content") you should first call addChildViewController
and only then addSubview
:
/// now
public func addAsChildViewController(vc: UIViewController, toView: UIView){
toView.addSubview(vc.view)
self.addChildViewController(vc)
vc.didMoveToParentViewController(self)
}
/// how it should be
public func addAsChildViewController(vc: UIViewController, toView: UIView){
self.addChildViewController(vc)
toView.addSubview(vc.view)
vc.didMoveToParentViewController(self)
}
I didn't experience any bugs, but still have some concerns. Should i submit PR?
I can't print when i use addAction method
Here's the code:
button.addAction { (sender) in
print("press")
}
why ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.