我有一个问题将我的 NSManagedObjectContext 暴露给 SwiftUI 的环境。这是我的代码:
extension SceneDelegate: UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let window = UIWindow(windowScene: windowScene)
let context = PersistentContainer.shared.viewContext
let rootView = TabBarView().environment(\.managedObjectContext, context)
window.rootViewController = UIHostingController(rootView: rootView)
self.window = window
window.makeKeyAndVisible()
}
}
class PersistentContainer: NSPersistentContainer {
static let shared = PersistentContainer()
private convenience init() {
self.init(name: "App")
viewContext.mergePolicy = NSMergePolicy(merge: .mergeByPropertyStoreTrumpMergePolicyType)
viewContext.automaticallyMergesChangesFromParent = true
loadPersistentStores { description, …Run Code Online (Sandbox Code Playgroud) I'v got a UITableView whose dataSource updated at random intervals in a very short period of time. As more objects are discovered, they are added to the tableView's data source and I insert the specific indexPath:
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
The data source is located in a manager class, and a notification is posted when it changes.
- (void)addObjectToDataSource:(NSObject*)object {
[self.dataSource addObject:object];
[[NSNotificationCenter defaultCenter] postNotification:@"dataSourceUpdate" object:nil];
}
Run Code Online (Sandbox Code Playgroud)
The viewController updates the tableView when it receives this notification. …
我想将ADInterstitialAds视为常规的UIView,以便在使用时获得一些灵活性.
例如,默认的插页式转换是slide up from the bottom of the screen.我不喜欢那样.是否可以首先将alpha值修改为0,然后将其更改为1?
另一个例子是在全屏添加前面有不同的UIView少量时间,即只是在转换过程中ADInterstitialAd?ADInterstitialAds往往是视图层次结构中最顶层的视图.如果我设法将ADInterstitialAds视为常规的UIView,我想我可以做到这一点.有关如何实现这一目标的任何建议?
我在下面定义的协议存在问题.我有两个要求:
Peer用作其他类中的类型,同时保持具体类的私有性.为了满足第二点,我需要使协议符合Equatable协议.但是当我这样做时,我不能再将其Peer用作类型,因为它需要被视为通用类型.这意味着我不能再具有私有的具体实现,并且要求1被打破.
想知道是否有其他人遇到过这个问题并以某种方式绕过它.也许我误解了我所得到的错误indexOf......
Group.swift
import Foundation
class Group {
var peers = [Peer]()
init() {
peers.append(PeerFactory.buildPeer("Buddy"))
}
func findPeer(peer: Peer) -> Bool {
if let index = peers.indexOf(peer) {
return true
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
Peer.swift
import Foundation
protocol Peer {
var name: String { get }
}
class PeerFactory {
static func buildPeer(name: String) -> Peer {
return SimplePeer(name: name)
}
}
private class SimplePeer: Peer {
let …Run Code Online (Sandbox Code Playgroud) 我有以下 JSON 对象:
{
"user_name":"Mark",
"user_info":{
"b_a1234":"value_1",
"c_d5678":"value_2"
}
}
Run Code Online (Sandbox Code Playgroud)
我已经JSONDecoder这样设置了:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
Run Code Online (Sandbox Code Playgroud)
我的Decodable对象看起来像这样:
struct User: Decodable {
let userName: String
let userInfo: [String : String]
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是该.convertFromSnakeCase策略正在应用于字典的键,我希望这种情况不会发生。
// Expected Decoded userInfo
{
"b_a1234":"value_1",
"c_d5678":"value_2"
}
// Actual Decoded userInfo
{
"bA1234":"value_1",
"cD5678":"value_2"
}
Run Code Online (Sandbox Code Playgroud)
我研究过使用自定义keyDecodingStrategy(但没有足够的信息来以不同方式处理字典)以及我的自定义初始值设定项Decodable结构的自定义初始值设定项(似乎此时键已经被转换)。
处理此问题的正确方法是什么(仅为字典创建键转换异常)?
注意:我更愿意保留蛇形大小写转换策略,因为我的实际 JSON 对象在蛇形大小写中有很多属性。我当前的解决方法是使用 CodingKeys 枚举手动进行蛇形大小写转换。
我使用ARC制作了一个iPhone应用程序,可以访问地址簿中的每个条目,然后是每个人的每个地址.数据存储在CFArrays中,CFArrays是免费的桥接到NSArrays.代码如下.
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSArray *peopleArray =[(__bridge NSArray *) arrayRef copy];
CFRelease(arrayRef);
arrayRef = nil;
for(id personId in peopleArray)
{
ABRecordRef person = (__bridge ABRecordRef) personId;
//process other attributes of the address book
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
CFArrayRef addressRef = ABMultiValueCopyArrayOfAllValues(multi);
NSArray *addressArray = [(__bridge NSArray *) addressRef copy];
for(NSDictionary *address in addressArray)
{
//process the addresses
}
CFRelease(addressRef);
addressRef = nil;
}
Run Code Online (Sandbox Code Playgroud)
从我在互联网和Apple的内存管理指南中研究过,这看起来是正确的方法.问题是当我运行代码时,它停止在"CFRelease(addressRef)"上,突出显示绿色,文本"Thread 1"(不确定这个错误意味着什么).我也尝试在for循环之前放置CFRelease,但同样的问题也出现了.
如果我删除了CFRelease,它会编译,但是在创建addressArray时会出现内存泄漏.有谁知道如何解决这个问题?我似乎无法用ARC来解决这个问题.
ios ×3
swift ×2
atomic ×1
cocoa ×1
codable ×1
core-data ×1
decodable ×1
iad ×1
interstitial ×1
iphone ×1
json ×1
jsondecoder ×1
memory-leaks ×1
swift2 ×1
swiftui ×1
uitableview ×1
uiview ×1