2015年12月24日 星期四

Swift - 常用檔案資料夾路徑的取得方式(Home資料夾,檔案資料夾,暫存資料夾)

iOS應用程式只能在自己的資料夾下進行檔案的操作,不可以訪問其他的存儲空間,此區域被稱為沙盒(sandbox)。下面介紹常用的檔案資料夾:

1,Home資料夾 ./
整個應用程式各檔案所在的資料夾

//得到Home資料夾路徑
let homeDirectory = NSHomeDirectory()


2,Documnets資料夾 ./Documents
使用者檔案資料夾,蘋果建議將應用程式在「執行中建立」或「在執行中瀏覽到的」檔案數據儲存在該資料夾下,iTunes備份或恢復的時候會包括此資料夾

//方法1
let documentPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
    NSSearchPathDomainMask.UserDomainMask, true)
let documnetPath = documentPaths[0] as! String

//方法2
let ducumentPath2 = NSHomeDirectory() + "/Documents"


3,Library資料夾 ./Library
這個資料夾下有兩個子資料夾:Caches 和Preferences
Library/Preferences資料夾,包含應用程式的偏好設置檔案。不應該直接創建偏好設置檔案,而是應該使用NSUserDefaults類來取得和設置應用程式的偏好。
Library/Caches資料夾,主要存放暫存檔案,iTunes不會備份此資料夾,此資料夾下檔案不會再應用程式退出時刪除

//Library資料夾-方法1
let libraryPaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory,
    NSSearchPathDomainMask.UserDomainMask, true)
let libraryPath = libraryPaths[0] as! String

//Library資料夾-方法2
let libraryPath2 = NSHomeDirectory() + "/Library"


//Cache資料夾-方法1
let cachePaths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory,
    NSSearchPathDomainMask.UserDomainMask, true)
let cachePath = cachePaths[0] as! String

//Cache資料夾-方法2
let cachePath2 = NSHomeDirectory() + "/Library/Caches"


4,tmp資料夾 ./tmp
用於存放暫存檔案,儲存應用程式再次啟動過程中不需要的資料,重啟後會清空。

//方法1
let tmpDir = NSTemporaryDirectory()

//方法2
let tmpDir2 = NSHomeDirectory() + "/tmp"


5,應用程式打包安裝的資料夾NSBundle.mainBundle()
專案打包安裝後會在NSBundle.mainBundle()路徑下,該路徑是只能讀的,不允許修改。
所以當我們專案中有一個SQLite資料庫要使用,在應用程式開啟時,我們可以把該路徑下的資料庫copy一份到Documents路徑下,以後整個專案都將使用Documents路徑下的資料庫。

//宣告一個Documents下的路徑
var dbPath = NSHomeDirectory() + "/Documents/hanggeDB.sqlite"
//判斷資料庫檔案是否存在
if !NSFileManager.defaultManager().fileExistsAtPath(dbPath){
    //取得安裝包內資料庫路徑
    var bundleDBPath:String? = NSBundle.mainBundle().pathForResource("hanggeDB", ofType: "sqlite")
    //將安裝包內資料庫copy到Documents資料夾下
    do{
        try NSFileManager.defaultManager().copyItemAtPath(bundleDBPath!, toPath: dbPath)
    }catch{}
}


參考來源

2015年12月19日 星期六

NSBundle.mainBundle().pathForResource("name", ofType: "type")找不到檔案的解決辦法

在使用NSBundle.mainBundle().pathForResource("fileName", ofType: "type")時,找不到其對應的檔案。
檔案一般是通過右鍵 > "Add Files to 專案名稱" 或直接拖曳的方式增加到該專案中,
但是使用NSBundle.mainBundle().pathForResource("fileName", ofType: "type")時,無論如何都找不到檔案的話,

可以試試用copy Bundle Resources下面的"+"號,手動將檔案加入到你的專案中,這樣也許能解決問題。

2015年11月11日 星期三

淡入淡出浮動式的回到網頁頂部按鈕


1.首先HTML部分請加在<body>內(wordpress則加在index.php與single.php)

<div id="gotop">˄</div>

2.CSS部分請加在您的css檔裡(wordpress則加在style.css)

#gotop {
    display: none;
    position: fixed;
    right: 20px;
    bottom: 20px;    
    padding: 10px 15px;    
    font-size: 20px;
    background: #777;
    color: white;
    cursor: pointer;
}

3.jQuery部分請加在您的css檔裡(wordpress則加在index.php與single.php)

<!-- Start 往頂部箭頭 Jquery碼//-->
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#gotop").click(function(){
jQuery("html,body").animate({
scrollTop:0
},1000);
});
$(window).scroll(function() {
if ( $(this).scrollTop() > 300){
$('#gotop').fadeIn("fast");
} else {
$('#gotop').stop().fadeOut("fast");
}
});
});
</script>


CSS微調

如果你想要調整箭頭改成圖片.可以到CSS裡
background: #777;
改成
background-image:url(你的圖片.jpg);
background-repeat: no-repeat;
其他如果想要調成圓角背景則是參考一般CSS按鈕設定即可
border-radius: 50%;

從 Swift 1.x 到 Swift 2.0

從 Swift 1.x 到 Swift 2.0 轉換時候,有什麼要注意的呢?

一般 Apple 的做法是新的 IDE 才會支援新的語法或是平台,所以要測試就拿新的 Xcode 7 來測試一下
不過在測試之前要先來討論一下,用 Xcode 6 開發 和用 Xcode 7 開發差別在哪?
對於開發者而言,最重要的事情就是 Xcode 可以支援的平台 OS 是幾版到幾版?
以 Apple 的做法來說,非常舊版本的 Xcode。以這次 iOS 9 的更新來說我用 Xcode 6 就無法把 App 部署到 iOS 9 的 Device 上面,所以來比較一下可以部署的平台
用 Xcode 7 打開一個專案如下可以看到

圖片無法 Copy 到下方的版本,Xcode 7 可以支援的就是
iOS 6.0 - 9.1
另外再來看一下 Xcode 6.4
同樣的地方
會看到是
iOS 6.0 - 8.4
換句話說,如果要寫 App 支援 iOS 6.0 之前的版本, Xcode 6.4也不能部署
要開發 iOS 9 請用 Xcode 7




接著回到正題,拿一個現成的 Project 來轉看看 這個 App 操作方式如下
 https://www.youtube.com/watch?v=gIyhpRXP8so
先用 Xcode 7 打開 

 就會看到轉換成最新 Swift  語言的提示
接著按下一步
接著選擇要檢視的 Target
PS. 如果沒有自動出現的話可以手動啟動這個 Convert 機制如下圖

接著就會出現比較的畫面,我們一一來看一下
第一個我們看到的不一樣
Swift 2.0 之後,都用  print() 來取代之前的 print() 和 println()
差別在 2.0 的 
print() 
代表 1.x 的 
println()
而 1.0 的 
print() 
在 2.0 要寫成 
print("Hello", terminator: "")
或者你也可以想成 2.0 的 
print() 
其實是
 print("Hello", terminator: "\n") 
的簡寫

接著看下一個
 這點是如果 var some = "Hello",對於 some 這個變數,如果之後的程式碼沒有用到,Xcode 就會強烈建議把 var 改成 let ,也就是會自動把 

var some = "Hello"

轉成

let some = "Hello"

 接著看下一個
這個原因是
在 1.x 的時候,有一個 
var name:String = "Michael"
 name 的型別是 String
如果我們想要用 for-in 走訪這個 string 把大寫的字找出來
在 1.x 會這樣寫

for c:Character in name {
    var str = "\(c)"
    if str == str.uppercaseString {
        print("\(str) is uppercase ")
    }
}

直接把 name 好像當成集合用, 
而在 2.0 會改成
for c:Character in name.characters {
    var str = "\(c)"
    if str == str.uppercaseString {
        print("\(str) is uppercase ")
    }
}
這樣比較合理一點,用 characters 來回傳一個由 Character 所組成的 Array 用 for-in來 iterate 所有的元素


有學員提供這個問題
在 1.x 的時候這樣寫

var urlStr = "http://www.apple.com"

var iphoneURL = urlStr.stringByAppendingPathComponent("iphone")

stringByAppendingPathComponent 本來是 NSString 常用的 method
Swift 的 String 也是可以用 
但是這樣 urlStr 裡面是 http:// 開頭的,也就是意圖要處理一個 URL
此時用 Xcode 7 打開,Convert 到新的 Syntax 就會出現下圖

也就是目前 Xcode 7 建議先轉成 NSString 再做 stringByAppendingPathComponent
這樣一來可以正常的執行
如果不聽,在 Xcode 7 就會直接給錯誤如下圖

 它就直接出現錯誤,stringByAppendingPathComponent 不可以在 String 用了,建議用 NSURL 來處理 path component 的問題



String 的 toInt() 被拿掉了如下圖
 在 2.0 如果要把數字集合而成的 String 轉成 Int 要就要用 Int 的 constructor
Int("12345")


/* by MusiCabbage */
好康道相報!!  SequenceType 本來一些奇怪的方法,變得更直覺了!!

例如說,本來我們想知道Array中有沒有某個Object,
必需這樣寫…

var array = ["a", "b", "c", "d", "e"]

contains(array, "a")

在 swift 2.0 的世界裡,我們只需要這樣寫…

array.contains("a")

類似的做法,CollectionType 裡面那些奇怪的方法,也一起跟進了!!
以前如果想要找到某個Object在Array中的Index,
是這樣寫…

var index = find(array, "c")

在 swift 2.0,完全換了個方法,但更容易懂了…

array.indexOf("c")


※以往的do-while 現在變成 repeat-while



※func 呼叫時,如有兩個以上的參數(含2個),第二個開始須帶外部參數名稱

func testX(a:Int,b:Int,c:Int)->String{
       let d = a+b+c
       return String(d)
}
print(testX(1,b:2,c:3))


你不必為第一個參數值再定義一個外部變數名稱:因為從函式名testX已經能很清楚地看出它的作用。但是第二個參數,就要被一個外部參數名稱所限定,以便在方法被呼叫時明確它的作用。

※使用class靜態屬性
class Test{
class var t1 = 10static var t1 = 10
}









參考來源

.htaccess應用

去除WWW RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.example\.com $ [ NC ] RewriteRule ^(.*) $ https: / /example.com/ $1 [ R = 301...