parent
af63582d43
commit
bc6d795f8b
@ -1,29 +0,0 @@ |
||||
// |
||||
// Alert.swift |
||||
// cloudmusic |
||||
// |
||||
// Created by Qihua Pan on 2020/7/3. |
||||
// Copyright © 2020 Qihua Pan. All rights reserved. |
||||
// |
||||
|
||||
import UIKit |
||||
import CoreData |
||||
extension UIViewController{ |
||||
func alert(message:String,title:String="警告信息",preferredStyle:UIAlertController.Style=UIAlertController.Style.alert,style:UIAlertAction.Style=UIAlertAction.Style.destructive){ |
||||
let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle) |
||||
|
||||
alertController.addAction(UIAlertAction(title: "确认", style:style, handler: nil)) |
||||
|
||||
self.present(alertController, animated: true, completion: nil) |
||||
} |
||||
|
||||
//获取上下文对象 |
||||
func getContext()-> NSManagedObjectContext { |
||||
//获取总代理 |
||||
let appDelegate = UIApplication.shared.delegate as! AppDelegate |
||||
//获取托管对象总管 |
||||
//let managedObjectContect = appDelegate.persistentContainer.viewContext |
||||
//返回托管对象总管 |
||||
return appDelegate.persistentContainer.viewContext |
||||
} |
||||
} |
@ -0,0 +1,94 @@ |
||||
// |
||||
// Alert.swift |
||||
// cloudmusic |
||||
// |
||||
// Created by Qihua Pan on 2020/7/3. |
||||
// Copyright © 2020 Qihua Pan. All rights reserved. |
||||
// |
||||
|
||||
import UIKit |
||||
import CoreData |
||||
extension UIViewController{ |
||||
func alertModal(message:String,title:String="警告信息",preferredStyle:UIAlertController.Style=UIAlertController.Style.alert,style:UIAlertAction.Style=UIAlertAction.Style.destructive){ |
||||
let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle) |
||||
|
||||
alertController.addAction(UIAlertAction(title: "确认", style:style, handler: nil)) |
||||
|
||||
self.present(alertController, animated: true, completion: nil) |
||||
} |
||||
|
||||
//获取上下文对象 |
||||
func getContext()-> NSManagedObjectContext { |
||||
//获取总代理 |
||||
let appDelegate = UIApplication.shared.delegate as! AppDelegate |
||||
//获取托管对象总管 |
||||
//let managedObjectContect = appDelegate.persistentContainer.viewContext |
||||
//返回托管对象总管 |
||||
return appDelegate.persistentContainer.viewContext |
||||
} |
||||
|
||||
func getMusic(id: Int32,name: String) -> Music { |
||||
let fetchRequest: NSFetchRequest = Music.fetchRequest() |
||||
fetchRequest.predicate = NSPredicate(format: "id == %@", NSNumber(value: id)) |
||||
do { |
||||
let result: [Music] = try self.getContext().fetch(fetchRequest) |
||||
debugPrint("count=\(result.count)") |
||||
if result.count > 0{ |
||||
return result[0] |
||||
}else{ |
||||
let music = NSEntityDescription.insertNewObject(forEntityName: "Music", into: self.getContext()) as! Music |
||||
music.id=id |
||||
music.name=name |
||||
music.status=false |
||||
return music |
||||
} |
||||
} catch { |
||||
fatalError(); |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
func clearAll(){ |
||||
let fetchRequest: NSFetchRequest = Music.fetchRequest() |
||||
let context=self.getContext() |
||||
do { |
||||
let result = try context.fetch(fetchRequest) |
||||
for item in result{ |
||||
context.delete(item) |
||||
} |
||||
do { |
||||
try self.getContext().save() |
||||
} catch { |
||||
let nserror = error as NSError |
||||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") |
||||
} |
||||
} catch { |
||||
fatalError(); |
||||
} |
||||
} |
||||
|
||||
func getPlayList()->[Music]{ |
||||
let fetchRequest: NSFetchRequest = Music.fetchRequest() |
||||
fetchRequest.predicate = NSPredicate(format: "status == true") |
||||
do { |
||||
let result: [Music] = try self.getContext().fetch(fetchRequest) |
||||
return result |
||||
} catch { |
||||
fatalError(); |
||||
} |
||||
|
||||
} |
||||
|
||||
// 更新数据 |
||||
func saveContext() { |
||||
do { |
||||
try self.getContext().save() |
||||
} catch { |
||||
let nserror = error as NSError |
||||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,34 @@ |
||||
// |
||||
// MyTabBar.swift |
||||
// cloudmusic |
||||
// |
||||
// Created by Qihua Pan on 2020/7/4. |
||||
// Copyright © 2020 Qihua Pan. All rights reserved. |
||||
// |
||||
|
||||
import UIKit |
||||
|
||||
|
||||
class MyTabBar: UITabBarController { |
||||
|
||||
static var myTabBar:MyTabBar? |
||||
|
||||
override func viewDidLoad() { |
||||
super.viewDidLoad() |
||||
// Do any additional setup after loading the view. |
||||
} |
||||
|
||||
|
||||
/* |
||||
// MARK: - Navigation |
||||
|
||||
// In a storyboard-based application, you will often want to do a little preparation before navigation |
||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { |
||||
// Get the new view controller using segue.destination. |
||||
// Pass the selected object to the new view controller. |
||||
} |
||||
*/ |
||||
|
||||
} |
||||
|
||||
|
@ -1,10 +1,13 @@ |
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="16119" systemVersion="19F101" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> |
||||
<entity name="Music" representedClassName="Music" syncable="YES" codeGenerationType="class"> |
||||
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES"/> |
||||
<attribute name="filepath" optional="YES" attributeType="String"/> |
||||
<attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/> |
||||
<attribute name="name" optional="YES" attributeType="String"/> |
||||
<attribute name="status" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/> |
||||
<attribute name="url" optional="YES" attributeType="String"/> |
||||
</entity> |
||||
<elements> |
||||
<element name="Music" positionX="-45" positionY="0" width="128" height="73"/> |
||||
<element name="Music" positionX="-45" positionY="0" width="128" height="118"/> |
||||
</elements> |
||||
</model> |
Loading…
Reference in new issue