336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


UIColor 비교하기


설명

두개의 UIColor 똑같은지 아닌지 비교 하는 코드 입니다. UITextView는 Placeholder(힌트 텍스트)를 UITextFeild처럼 따로 지원 해주지 않기 때문에 텍스트 색을 비교해서 현재 텍스트가 힌트라는걸 판별 할때 저는 사용합니다.


사용환경

* Swift 4.2
* XCode 10.0

코드

//Objective-C
- (BOOL)color:(UIColor *)color1 isEqualToColor:(UIColor *)color2 withTolerance:(CGFloat)tolerance {
    CGFloat r1, g1, b1, a1, r2, g2, b2, a2;
    [color1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
    [color2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];

    return
    fabs(r1 - r2) <= tolerance &&
    fabs(g1 - g2) <= tolerance &&
    fabs(b1 - b2) <= tolerance &&
    fabs(a1 - a2) <= tolerance;

}

//Swift
func isColor(_ color:UIColor, color2:UIColor, tolerance:CGFloat) -> Bool {
        var r1 : CGFloat = 0, g1 : CGFloat = 0, b1 : CGFloat = 0, a1 : CGFloat = 0
        var r2 : CGFloat = 0, g2 : CGFloat = 0, b2 : CGFloat = 0, a2 : CGFloat = 0

        color.getRed(&r1, green:&g1, blue:&b1, alpha:&a1)
        color2.getRed(&r2, green:&g2, blue:&b2, alpha:&a2)

        return abs(r1-r2) <= tolerance && abs(g1-g2) <= tolerance && abs(b1-b2) <= tolerance && abs(a1-a2) <= tolerance
    }

맞치며

막상 포스팅을 하고나니깐 생각보다 활용을 할만한 곳이 생각이 안 나는거 같습니다,, 궁금하신점이나 틀린점이 있으면 댓글로 남겨주세요 감사합니다 :)



336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


앱스토어에서 어플 정보 받아오기


설명

이번 포스팅은 앱스토어에 올라가있는 어플에 대해서 정보를 가져오는 방법입니다. 생각보다 많이 사용이 될까 생각이 들지만 단순 버전관리라던지 여러가지로 사용 할수 있습니다. (저는 버전관리때문에 사용해 봤습니다.)
http://itunes.apple.com/lookup?bundleId=번들 아이디
이 조합으로 쉽게 텍스트 파일을 받아온 후 JSON 형식으로 받아서 사용하면 됩니다.


사용환경

* Swift 4.2
* XCode 10.0

코드

//Objective-C
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString* appID = infoDictionary[@"CFBundleIdentifier"];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
NSData* data = [NSData dataWithContentsOfURL:url];
NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

if ([lookup[@"resultCount"] integerValue] == 1){
    NSString* appStoreVersion = lookup[@"results"][0][@"version"];
     NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];

    NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
}

//Swift
guard let infoDic = Bundle.main.infoDictionary,
            let appID = infoDic["CFBundleIdentifier"] as? String,
            let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID)") else {
            print("gaurd error")
            return
        }

        do {
            let data = try Data(contentsOf: url)
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            guard let dic = json as? NSDictionary else {
                print("guard error")
                return
            }
            if dic.object(forKey: "resultCount") as? Int != 0 {
                //Do Something
                print(dic)
            } else {
                print("not result")
            }
        } catch {
            print(error.localizedDescription)
        }

맞치며

해당 아이디의 앱스토어에 올라가있는 정보입니다. 생각보다 많은 정보를 내려줍니다. 앱스토어 홈페이지가 아니 다른 사이트에서 보여줄때 이런식으로 정보를 받아와서 보여주는거 같습니다. 여담이지만 JSON은 아직까지 라이브러리 통해서 쓰는게 나은거 같습니다.. 틀린점이나 궁금한점 있으면 댓글 남겨주세요. 감사합니다 :)



336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


문자열의 처음, 마지막 지정된 개수만큼 지우기


설명

이번 포스팅은 문자열의 앞뒤 문자를 지우는 메소드 입니다.
문자열의 길이 만큼만 자를수 있기때문에 고정된 길이의 문자열에서만 사용해야됩니다. 안 그럴경우 Range over로 인해 앱이 튕기게 됩니다. 간단하게 dropLast(:), dropFirst(:)의 메소를 이용해서 자를수 있습니다.


사용환경

* Swift 4.2
* XCode 10.0

코드

let str = "You Know Programing?"
print("original = \(str)") //output = original = You Know Programing?
let drop_last = String(str.dropLast(3))
print("Last Drop index 3 = \(drop_last)") //output = Last Drop index 3 = You Know Programi
let drop_first = String(str.dropFirst(3))
print("First Drop index 3 = \(drop_first)") // output = First Drop index 3 =  Know Programing?

맞치며

문자열 길이에 따라 앱이 죽을수도있기 때문에 자르려고 하는 길이가 문자열 길이보다 적은지 항상 확인 해주고 사용해야됩니다.
궁금한점이나 틀린점이 있다면 댓글 남겨주세요. 감사합니다 :)



+ Recent posts