Wrappers for the Cocoa frameworks on macOS
—
Low-level C API framework providing fundamental data types, collections, memory management, and system services. CoreFoundation forms the foundation layer for all higher-level Cocoa frameworks on macOS.
Static functions for creating Core Foundation collection objects from Python data structures, providing efficient bridging between Python and Objective-C collections.
def CFArrayCreate(allocator, values, numvalues, callbacks):
"""
Creates an immutable NSArray from Python sequence.
Args:
allocator: Memory allocator (typically None)
values: Sequence of objects to include in array
numvalues (int): Number of values to include from sequence
callbacks: Callback structure (must be None)
Returns:
NSArray: Immutable array containing the specified values
"""
def CFArrayCreateMutable(allocator, capacity, callbacks):
"""
Creates a mutable NSMutableArray.
Args:
allocator: Memory allocator (typically None)
capacity (int): Initial capacity hint (ignored)
callbacks: Callback structure (must be None)
Returns:
NSMutableArray: Empty mutable array
"""
def CFDictionaryCreate(allocator, keys, values, numValues, keyCallbacks, valueCallbacks):
"""
Creates an immutable NSDictionary from key-value pairs.
Args:
allocator: Memory allocator (typically None)
keys: Sequence of dictionary keys
values: Sequence of dictionary values
numValues (int): Number of key-value pairs to include
keyCallbacks: Key callback structure (must be None)
valueCallbacks: Value callback structure (must be None)
Returns:
NSDictionary: Immutable dictionary with key-value pairs
"""
def CFDictionaryCreateMutable(allocator, capacity, keyCallbacks, valueCallbacks):
"""
Creates a mutable NSMutableDictionary.
Args:
allocator: Memory allocator (typically None)
capacity (int): Initial capacity hint (ignored)
keyCallbacks: Key callback structure (must be None)
valueCallbacks: Value callback structure (must be None)
Returns:
NSMutableDictionary: Empty mutable dictionary
"""
def CFSetCreate(allocator, values, numvalues, callbacks):
"""
Creates an immutable NSSet from Python sequence.
Args:
allocator: Memory allocator (typically None)
values: Sequence of objects to include in set
numvalues (int): Number of values to include from sequence
callbacks: Callback structure (must be None)
Returns:
NSSet: Immutable set containing unique values
"""
def CFSetCreateMutable(allocator, capacity, callbacks):
"""
Creates a mutable NSMutableSet.
Args:
allocator: Memory allocator (typically None)
capacity (int): Initial capacity hint (ignored)
callbacks: Callback structure (must be None)
Returns:
NSMutableSet: Empty mutable set
"""Functions for creating Core Foundation string objects with proper encoding and memory management.
def CFSTR(string):
"""
Creates an NSString from Python string.
Args:
string (str): Python string to convert
Returns:
NSString: Core Foundation string object
"""Functions for retrieving localized strings from application bundles, supporting internationalization and localization workflows.
def CFCopyLocalizedString(key, comment):
"""
Gets localized string from main application bundle.
Args:
key (str): Localization key
comment (str): Developer comment for translators
Returns:
str: Localized string or key if not found
"""
def CFCopyLocalizedStringFromTable(key, table, comment):
"""
Gets localized string from specific localization table.
Args:
key (str): Localization key
table (str): Name of localization table (.strings file)
comment (str): Developer comment for translators
Returns:
str: Localized string or key if not found
"""
def CFCopyLocalizedStringFromTableInBundle(key, table, bundle, comment):
"""
Gets localized string from table in specific bundle.
Args:
key (str): Localization key
table (str): Name of localization table
bundle: NSBundle object containing localization
comment (str): Developer comment for translators
Returns:
str: Localized string or key if not found
"""
def CFCopyLocalizedStringWithDefaultValue(key, table, bundle, value, comment):
"""
Gets localized string with fallback default value.
Args:
key (str): Localization key
table (str): Name of localization table
bundle: NSBundle object containing localization
value (str): Default value if localization not found
comment (str): Developer comment for translators
Returns:
str: Localized string, default value, or key
"""Constants defining callback structures for Core Foundation collections, typically set to None for standard object behavior.
kCFTypeArrayCallBacks: None
kCFTypeDictionaryKeyCallBacks: None
kCFTypeDictionaryValueCallBacks: None
kCFTypeSetCallBacks: Noneimport CoreFoundation
# Create an immutable array
python_list = ["apple", "banana", "cherry"]
cf_array = CoreFoundation.CFArrayCreate(python_list)
# Create a mutable dictionary
cf_dict = CoreFoundation.CFDictionaryCreateMutable()
# Dictionary can be manipulated using NSMutableDictionary methods
# Create a set from unique values
python_items = [1, 2, 2, 3, 3, 3]
cf_set = CoreFoundation.CFSetCreate(python_items) # Contains {1, 2, 3}import CoreFoundation
# Create Core Foundation strings
title = CoreFoundation.CFSTR("Application Title")
message = CoreFoundation.CFSTR("Hello, World!")
# Use in Core Foundation APIs that require CFStringRefimport CoreFoundation
# Get localized strings
title = CoreFoundation.CFCopyLocalizedString("APP_TITLE", "Main window title")
error_msg = CoreFoundation.CFCopyLocalizedStringFromTable(
"NETWORK_ERROR",
"Errors",
"Network connection failed"
)
# Use with default fallback
welcome = CoreFoundation.CFCopyLocalizedStringWithDefaultValue(
"WELCOME_MESSAGE",
"Main",
None, # main bundle
"Welcome!", # default value
"Welcome message for new users"
)The CoreFoundation module provides the foundation layer that all other Cocoa frameworks build upon. Collections created with these functions are fully compatible with Foundation and AppKit frameworks:
import CoreFoundation
import Foundation
# Create array using Core Foundation
cf_array = CoreFoundation.CFArrayCreate(["item1", "item2", "item3"])
# Use with Foundation methods
count = cf_array.count() # NSArray method
first_item = cf_array.objectAtIndex_(0) # NSArray method
# Core Foundation and Foundation objects are toll-free bridged
foundation_array = Foundation.NSArray.arrayWithObjects_("a", "b", "c", None)
# Can be used interchangeably with CF functionsAll Core Foundation objects are bridged to their corresponding Foundation types:
# Core Foundation types map to Foundation classes
CFArrayRef = NSArray
CFMutableArrayRef = NSMutableArray
CFDictionaryRef = NSDictionary
CFMutableDictionaryRef = NSMutableDictionary
CFSetRef = NSSet
CFMutableSetRef = NSMutableSet
CFStringRef = NSStringInstall with Tessl CLI
npx tessl i tessl/pypi-pyobjc-framework-cocoa