我有一个函数从特定实体中获取满足指定标准的那些对象:
func fetchWithPredicate(entityName: String, argumentArray: [AnyObject]) -> [NSManagedObject] {
let fetchRequest = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = NSPredicate(format: "%K == %@", argumentArray: argumentArray)
do {
return try self.managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
} catch {
let fetchError = error as NSError
print(fetchError)
return [NSManagedObject]()
}
}
Run Code Online (Sandbox Code Playgroud)
当我传递一个包含多个aguments(多个属性名称和值)的数组时,似乎会创建一个这样的查询:
attributeName1 = value1 OR attributeName2 = value2 OR attributeName3 = value3 OR...
Run Code Online (Sandbox Code Playgroud)
我想为AND更改这些OR.
编辑:
问题是它只用"%K ==%@" 替换了前两个项目.
所以,我必须创建一个NSCompoundPredicate来动态创建谓词.
我有一个UITabBarController有4个项目标签.其中之一是AvCaptureVideoPreviewLayer(是条形码扫描仪).我想要做的是仅为AVCaptureVideoPreviewLayer(如iOS相机应用程序)禁用自动item bar旋转,而不是与屏幕一起旋转的自动旋转.这有点困难,因为我认为UITabBarConrtoller不允许你轻松禁用旋转.
我的相机视图的代码是:
import UIKit
import AVFoundation
class ScannerViewController: UIViewController,
// MARK: Properties
/// Manages the data flow from the capture stage through our input devices.
var captureSession: AVCaptureSession!
/// Dispays the data as captured from our input device.
var previewLayer: AVCaptureVideoPreviewLayer!
// MARK: Methods
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blackColor()
self.captureSession = AVCaptureSession()
// This object represents a physical capture device and the properties associated with that …Run Code Online (Sandbox Code Playgroud) 我有一个UITabBarController子类,它具有以下代码:
class TabBarController: UITabBarController {
// MARK: Methods
override func viewDidLoad() {
super.viewDidLoad()
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
print(self.selectedIndex)
if self.selectedIndex == 1 {
return .Portrait
}
return .All
}
}
Run Code Online (Sandbox Code Playgroud)
该功能supportedInterfaceOrientations仅在我从iphone执行应用程序时调用.如果我从我的iPad mini执行该应用程序,则不会调用它.知道这种行为的原因吗?
随着Xcode 7.3的新更新,出现了许多与Swift 3新版本相关的问题.其中一个问题是"C-style for statement已被弃用,将在未来版本的Swift中删除"(这在传统版本中出现)for语句).
其中一个循环有多个条件:
for i = 0; i < 5 && i < products.count; i += 1 {
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否有任何优雅的方式(不使用break)在Swift的for-in循环中包含这个双重条件:
for i in 0 ..< 5 {
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Hiberante 5和Spring 4.2.3.我找不到将eventListener添加到SessionFactory范围的方法.我只需要在hibernate持久化对象之前设置一个日期.我在spring.xml中定义了sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/hibernate/mapping/User.hbm.xml</value>
<value>com/hibernate/mapping/PublicKey.hbm.xml</value>
<value>com/hibernate/mapping/File.hbm.xml</value>
<value>com/hibernate/mapping/Url.hbm.xml</value>
<value>com/hibernate/mapping/Role.hbm.xml</value>
<value>com/hibernate/mapping/Operation.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.timeout">100</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="hibernate.c3p0.validate">true</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我有我的GenericDAOImpl来获取这个sessionFactory:
public abstract class GenericDAOImpl <T, PK extends Serializable> implements GenericDAO<T, PK> {
private SessionFactory sessionFactory;
/** Domain class the DAO instance will be responsible for */
protected Class<T> type;
@SuppressWarnings("unchecked")
public GenericDAOImpl() …Run Code Online (Sandbox Code Playgroud) 我试图在钥匙串中保存一个数组但我无法将数组转换为NSData.我有我的函数准备保存字符串,但我不知道如何valueData从数组中获取.
func add(key: String, value: AnyObject) {
let service = NSBundle.mainBundle().bundleIdentifier!
let valueData: NSData! = value.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false)
let secItem = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrService as String : service,
kSecAttrAccount as String : key,
kSecValueData as String : valueData
]
let result: UnsafeMutablePointer<AnyObject?> = nil
let status = Int(SecItemAdd(secItem, result))
if status == Int(errSecDuplicateItem){
self.update(key, newData: value)
} else {
print("An error occurred with code \(status)")
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个视图,当键盘出现时调整约束.所以当键盘出现并消失时我会收到通知.
键盘已经显示并旋转屏幕时会出现上述行为.然后发生下一个动作:
因此,updateView函数接收第一个高度,然后接收不同的高度.这导致视图的奇怪移动调整值的两倍.
override func viewDidLoad() {
super.viewDidLoad()
// Creates notification when keyboard appears and disappears
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
self.adjustingHeight(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
self.adjustingHeight(false, notification: notification)
}
private func adjustingHeight(show: Bool, notification: NSNotification) {
// Gets notification information in an dictionary
var userInfo = …Run Code Online (Sandbox Code Playgroud)